javascript advice on forms

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
kol090
Forum Newbie
Posts: 5
Joined: Mon Sep 19, 2005 8:46 am

javascript advice on forms

Post by kol090 »

Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
can you pls tell me what i have done wrong here?
i want to add input1 and input2 together and then display it in the msgbox, 
the strange thing is that it works when i want to subtract multiply and divide but not add

Code: Select all

<script type="text/javascript">
function valid(form)
{
  var input1=0;
  var input2=0;
  var tot = 0;
  input1 = document.myform.data1.value;
  input2 = document.myform.data2.value; 
  tot = input1 + input2;
  alert("Hello " + tot + "! Welcome...");
}
</SCRIPT>



Click on the button after entering your
name into the text box:<BR>

<form name="myform">
<input type="text" name="data1" value="" size=10>
<input type="text" name="data2" value="" size=10>
<INPUT TYPE="button" VALUE="Click Here" onClick="valid(this.form)">
</form>
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Attributes are treated as strings.. you need to convert them to integers or floats: parseInt() or parseFloat()

Code: Select all

input1 = parseInt(document.myform.data1.value);
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

EDIT: Maybe I should read the posts properly :roll: @ me.

+ is the concatenate operator when used on strings.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

this might be common in many languages

Code: Select all

<script type = 'text/javascript'>
var i = 3, j = 5, k;
var l = "str";
var m = "500";

k = i + j; //returns an i nteger; output should be 8
document.write(k + "<br />");
k = 3 + l; //returns a string since one of the operands is a string; output should be 3str
document.write(k + "<br />");
k = l + m;//returns string since both operands are strings; output should be str500
document.write(k + "<br />");
k = i + m; //returns 3500
document.write(k + "<br />");
k = i + parseInt(m); //returns 503
document.write(k + "<br />");
</script>
kol090
Forum Newbie
Posts: 5
Joined: Mon Sep 19, 2005 8:46 am

Post by kol090 »

i tried

Code: Select all

input1 = parseInt(document.myform.data1.value);
but nothing works i can't even get one of the things in entered back forget about adding them.

Code: Select all

function sum(form) 

{
    var input1 = parseInt(document.myform.data1.value);
	var input2 = parseInt(document.myform.data2.value);
    alert("Hello " + input2 + " ! Welcome...");
}


</script>
<form>
	   <input type="text" name="data1" size ="2" onBlur="checksuma(this.value)">
       <input type="text" name="data1" size ="2" onBlur="checksumb(this.value)">
	   <input type="button" value="sum" onClick="sum(this.form)">
</form>

whats the matter?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

see you are getting the form object as form in the function not as myform.
so change myform in your assignment statements to form.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to client-side.
kol090
Forum Newbie
Posts: 5
Joined: Mon Sep 19, 2005 8:46 am

Post by kol090 »

alright thanks it works now
Post Reply