Page 1 of 1

drop down box problem

Posted: Wed Sep 12, 2007 8:16 pm
by sarris
hi there,
i am comparing the values of two drop down boxes and it seems that even if the values are numeric the comparisson is held on alphabetic base. does anyone know why this is happening?
my two drop down boxes are identical

Code: Select all

<select name="price_min" id="price_min" onchange = "checkValidValues(this,document.getElementById('price_max'),'price')" >
	<option value=-1 selected>any</option>
	<option value=200>200 €</option>
	<option value=400>400 €</option>
	<option value=600>600 €</option>
	<option value=800>800 €</option>
	<option value=1000>1000 €</option>
	<option value=1500>1500 €</option>
	<option value=2000>2000 €</option>
	<option value=2500>2500 €</option>
</select>

<select name="price_max" id="price_max" onchange = "checkValidValues(document.getElementById('price_min'),this,'price')">
	<option selected value=-1>any</option>
	<option value=200>200 €</option>
	<option value=400>400 €</option>
	<option value=600>600 €</option>
	<option value=800>800 €</option>
	<option value=1000>1000 €</option>
	<option value=1500>1500 €</option>
	<option value=2000>2000 €</option>
	<option value=2500>2500 €</option>
	<option value=3000>3000 €</option>
</select>
and the function tha makes the comparisson

Code: Select all

function checkValidValues(a_drop,b_drop,size_or_price){
	//if flag = 1 then its about price
	//if flag = 2 then its about size

	
	var minimum = a_drop.options[a_drop.selectedIndex].value;
	var maximum = b_drop.options[b_drop.selectedIndex].value;
	if(minimum != -1 && maximum != -1){
		if(minimum > maximum){
			alert("Minimum " + size_or_price + " can't be bigger than maximum");
			b_drop.selectedIndex = 0;
		}
	}
}
if i choose 200 for the first and 1000 for the second i get the alert message
if i choose 200 for the first and 2500 for the second i dont get it

Posted: Wed Sep 12, 2007 8:45 pm
by feyd
You're comparing strings, not numbers. They need to be converted to numbers.

Posted: Thu Sep 13, 2007 6:38 am
by vinoth
hi sarris
Use eval() function before comparision
Then it treated as numbers and return the result

Posted: Thu Sep 13, 2007 7:18 am
by feyd
No eval(). There are methods called parseFloat() and parseInt() as well as the Number constructors, if I remember correctly.