tags:

views:

47

answers:

3

My dom looks like:

<td>

<select id=s1 class=c1>...</select>
<select id=s2 class=c1>...</select>
<select id=s3 class=c1>...</select>
<input type=button id=btn value=click/>
</td>

Now when the page loads, I do a $(".c1").hide(); and then based on other logic, make one of them visible.

Now when the button is clicked, I want the ID of the drop down list that is currently visible.

How can I do this?

+2  A: 
$("select.c1:visible").attr("id")

Should return you the ID of the first visible select element.

Also please quote your attribute values.

Matti Virkkunen
A: 
$('#btn').click( function(){

    $('select.c1:visible').get(0).id

});
redsquare
A: 
$("select:visible").attr("id")

BTW, you should be aware that IDs are not the only way to find and handle elements in JavaScript. I mean that $("select:visible") already contains the element itself!

Álvaro G. Vicario