views:

1932

answers:

3

Ok.

html:

<div class="selector">
    <select name="something"> //this is what myVar is referring to.
        <option>stuff</option>
    </select>
</div>

Say I have var:

var myVar = $(".selector").find("[name=something]");

And I want to concatenate with option:selected:

$(myVar here + " option:selected").change(function(){
    //do something
});

I can't seem to make this work. Thanks to anyone who can help.

A: 

On your edited version you've removed val(), so myVar is a jQuery object. You can do:

myVar.find("option:selected")

or better

$("option:selected", myVar)
Kobi
Yea the change event does work for this. Haven't had a problem with that. I'm going to edit for more clarity.
Scott
I need to stick with classes...the div "selector" is generated from $.post. And there will be more than one in the code.
Scott
Yea..made mistake..the select menu has no value..sorry. I've updated the example to show that.
Scott
I like the looks of your last suggestion...I'll try it. Thanks.
Scott
I it's straight hierarchy, you can also do `$(".selector [name=something] option:selected")` - one selector, no `find`, all classes.
Kobi
@Kobi...thanks alot! Your last suggestion did the trick :)
Scott
no problem, was it the answer or the comment? I'll edit the answer to be clearer.
Kobi
`$(" option:selected", myVar)` Just wanted to add...that there needs to be a space before `option:selected` in order for it to work correctly. Thanks for all your help!
Scott
Leading space? That is... strange. I'll check that later. Thanks.
Kobi
Sorry @Kobi...it works either way with/without space. I just tried it.
Scott
Ok, good to know. That sort of things can end in bug reports. Thanks. Have fun!
Kobi
A: 

That should work (assuming the word 'here' isn't in the code). What does the the element with name = something contain as a value? what are the elements you expect to get using the jquery method?

Marius
Sorry..err the select menu in the example has no val..edited.
Scott
A: 

Simply the selector method may not return you only one element, this may be better way to use jQuery.

 $("select").change(function () {

          $("select option:selected").each(function () {
                // do something
              });

        })
        .trigger('change');
Jirapong