drop down box problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

drop down box problem

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're comparing strings, not numbers. They need to be converted to numbers.
vinoth
Forum Contributor
Posts: 113
Joined: Thu Aug 02, 2007 3:08 am
Location: India
Contact:

Post by vinoth »

hi sarris
Use eval() function before comparision
Then it treated as numbers and return the result
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

No eval(). There are methods called parseFloat() and parseInt() as well as the Number constructors, if I remember correctly.
Post Reply