Bug Tracker

Opened 13 years ago

Closed 12 years ago

Last modified 6 years ago

#1474 closed bug (fixed)

setting selectedIndex doesn't update dropdown position

Reported by: eXcel Owned by:
Priority: minor Milestone: 1.2
Component: core Version: 1.1.3
Keywords: form dropdown selectedIndex attr Cc:
Blocked by: Blocking:

Description

On my webpage, I noticed that if i did something like this

$('myDropdown').attr('selectedIndex',3);

the dropdown position would not change; however

alert($('myDropdown').attr('selectedIndex'));

prints 3

To see the correct behavior try something like this:

document.getElementById('myDropdown').selectedIndex = 3; 

which does change the dropdown position

use with

<form>
<select name="myDropdown" id="myDropdown">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
</select>
</form>

Tested in Firefox 2.0.0.6

Change History (10)

comment:1 Changed 12 years ago by brandon

Resolution: fixed
Status: newclosed

Fixed in SVN Rev: [3387]

comment:2 Changed 9 years ago by rosali

It looks like this bug is back again since jQuery 1.6.1.

comment:3 in reply to:  2 Changed 9 years ago by chiu0602+bugs_jquery@…

Replying to rosali:

It looks like this bug is back again since jQuery 1.6.1.

It happened on jQuery 1.6.2 too

comment:4 Changed 9 years ago by anonymous

Yep. It took me an hour of fiddling to finally realize that.

comment:5 Changed 9 years ago by dmethvin

Most likely you are using .attr() instead of .prop(). There is no *attribute* named selectedIndex so the code above is invalid.

Last edited 9 years ago by dmethvin (previous) (diff)

comment:6 in reply to:  5 Changed 8 years ago by anonymous

Replying to dmethvin:

Most likely you are using .attr() instead of .prop(). There is no *attribute* named selectedIndex so the code above is invalid.

Using .prop doesn't change anything, still doesn't work.

comment:7 Changed 8 years ago by prakashsinha12@…

You Can get Selected index like this

$('myDropdown').get(0)('selectedIndex',3);

comment:8 in reply to:  7 Changed 8 years ago by anonymous

Replying to prakashsinha12@…:

You Can get Selected index like this

$('myDropdown').get(0)('selectedIndex',3);

When using an asp.net dropdownlist (<asp:DropDownList), when I tried to set a selectedIndex on the dropdownlist using the above example, it does not work.

comment:9 Changed 8 years ago by dermotmcevoyrockey@…

I have declare the asp:dropdownlist object as $("#<%=ddlCity.ClientID %>").get(0)('selectedIndex',3);

comment:10 Changed 6 years ago by anonymous

14/04/2014

$('#login').prop('selectedIndex', index);

this works for me :)

Note: See TracTickets for help on using tickets.