Tuesday, 15 June 2010

html - javascript textarea value and if -



html - javascript textarea value and if -

i utilize next code:

<html> <head> <script type="text/javascript"> function asd(){ var b = document.getelementbyid("txt").value; var c = document.getelementbyid("txt2").value; if( b > c ){alert("the first value more sec value");} } </script> </head> <body> <textarea id="txt"></textarea> <input type="button" value="click me" onclick=asd()> <br> <textarea id="txt2"></textarea> </body> </html>

but codes work incorrectly. writing firs textarea, 5. i'm writng scnd textarea , 40. , alarm works. ı dont understand. searched , find solution.

if( parseint(b,10)) > (parseint(c,10)) )

so why has failed first time?

it failed first time because numbers parsed strings.

var b = document.getelementbyid("txt").value; //b = "5" var c = document.getelementbyid("txt2").value; // c = "40" if( b > c ){ // "5" > "40" false because browser not understand this. alert("the first value more sec value"); }

if utilize parseint strings parsed integer.

so:

var b = document.getelementbyid("txt").value; //b = "5" var d = parseint(b); // d = 5

the 'is greater/less than' sign work integers ( , floats etc.) not strings. that's why if-statement returned false.

javascript html

No comments:

Post a Comment