Philll_t
Philll_t

Reputation: 4437

How to get the value of a drop down selected value with the parent as the selected element in jQuery

History:

I'm using the children method because I'll be looping through divs that have content in them that is dynamically generated by the user, so there's no way of me knowing how many divs there will be. If there is another method I should be using, by all means, show me :)

I'm having trouble here using the following code:

$("[name = listItem]").each( function (){
var z = $(this).children("[name='dropDown'] option:selected").val();
alert(z);
});

In this example the output would be an alert box with:

undefined

It's strange because this way bellow works fine!

var z = $("[name='dropDown'] option:selected").val();
alert(z);

the output for this would be the appropriate value:

1

for the children() method, I thought you could use the standard selector syntax. What do you think?

Upvotes: 1

Views: 1482

Answers (1)

472084
472084

Reputation: 17894

Try:

$("[name = listItem]").each( function (){
    var z = $(this).find("option:selected").val();
    alert(z);
});

Or:

$("[name='dropDown'] option:selected").each( function (){
    var z = $(this).val();
    alert(z);
});

But i am a bit confused as to what listItem & dropDown are...

Upvotes: 2

Related Questions