views:

33

answers:

2

First the code might suck as I'm new with JQuery. I have this html:

<select name="numberItems" id="id_numberItems"> 
<option value="1" selected="selected">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
</select>
<fieldset class="dropdownList"> 
     [...]
</fieldset>
<fieldset class="dropdownList"> 
     [...]
</fieldset>  <!-- This fieldset is repeated 5 times -->

This is my Jquery code:

// Dropdown
function dropDown(val) {
    $('.dropdownList').hide();
    $('.dropdownList:lt(' + val + ')').show();
}

$("#id_numberItems").click(function() { dropDown($('#id_numberItems').val()) });

This dropDown thing is working on Firefox, but not in Google Chrome ¿why? Thanks

A: 

Try using the change event.

bazmegakapa
+1  A: 

Attach your dropDown-function to the change-event of your dropdown and it should work the way you want it (or as my crystal ball suggests me...)

$("#id_numberItems").change(function() { dropDown($('#id_numberItems').val()) });

example on jsbin.com

john_doe
You are right, can you tell me why this happens? thanks
maraujop
I'm not sure what the spec says about the onclick-behaviour of a select-element. Chrome won't invoke the click-event of the select when you click an option as the other browsers are. On the first click after page loading you get '1' as value because of the "pre-selection". Next time you click the select you get the value of the previous selection and so on. So if there is no special reason you should always use the onchange-event.
john_doe