my html code is like:
When you compare two strings in an if statement:
if ( l > h) {}
when l = "12" and h = "112"
It compares these two values as strings as "3" > "1"
. It says l > h
is true
You need to convert them to numbers:
if(+l > +h){
The unary plus operator will convert the string value to numeric value. See this question for more details:
What's the significant use of unary plus and minus operators?
Live example.
try like this :-
if(parseInt(l,10)>parseInt(h,10))
or for floating Numbers you can use
if(parseFloat(l,10)>parseFloat(h,10))
or simply use
Number()
Use parseInt with base to get correct results:
var l = parseInt(document.test.low.value, 10);
var h = parseInt(document.test.high.value, 10);
var lnum = new Number(l);
var hnmu = new Number(h);
if(lnum > hnum){
alert('if');
}else{
alert('else');
}