tags:

views:

60

answers:

5

How would I use $(this) instead of the full selector in the code below in the "if" statement. I tried a few different combination of "this" usage and none worked. Among them are...

if ($("this :selected").text().indexOf("[Subtract") >= 0){
if ($(this" :selected").text().indexOf("[Subtract") >= 0){

I am sure its gotta be something really simple, I just cannot figure it out today.

<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("select[name^=SELECT___]").change(function() {
if ($("select[name^=SELECT___] :selected").text().indexOf("[Subtract") >= 0){
alert('');
}
});
});
</script>
+3  A: 

Use the find method on $(this) to find specific elements that are descendants of this:

$(this).find(":selected").text().indexOf("[Subtract")
Gumbo
.find() searches all the hierarchy
Tudorizer
I'll give it to you cause you were first. Thanks. Worked!!!!
@Tudorizer: Just like the selector `select[name^=SELECT___] :selected`.
Gumbo
So which one would be better find or filter, hmmm looks like a trip to the jQuery docs
@user357034: My solution is equivalent to `select[name^=SELECT___] :selected` (note the space) while Tudorizer’s is equivalent to `select[name^=SELECT___]:selected` (note the missing space).
Gumbo
BTW, user357034, your two examples above are syntactically wrong. `this` is an object.
Tudorizer
@Tudorizer: He asked us because all his attempts failed.
Gumbo
@Gumbo, yes. I'm just helping him out, so he's more careful in the future. Sometimes stuff like that gets really frustrating.
Tudorizer
Well yes I know that otherwise I wouldn't be here :)
Using $(':selected', this) like suggested below is more efficient because it does not create a jquery wrapper over the context (which is not necessary)
Andre Haverdings
@Andre - That's just not true. :o) As I stated in [my answer](http://stackoverflow.com/questions/3933549/use-this-instead-of-selector/3933597#3933597), it gets turned around into the *exact* same structure as @Gumbo's answer. This happens after a bunch of tests run. After it gets turned around, it starts all over. So setting the context is less efficient. [Here's the source.](http://github.com/jquery/jquery/blob/1.4.2/src/core.js#L146)
patrick dw
@patrick, freak.. you're right, that's just stupid.. this might be where jquery could gain performance; instead of converting the context to an array with jquery context and then for each dom element in the array call Sizzle.find ...
Andre Haverdings
+2  A: 

$(this).filter(':selected')

http://api.jquery.com/filter/

Tudorizer
This is incorrect. `.filter()` will test only against the current set. Not any descendants.
patrick dw
I know. If what you said is the need, then .find() is the right answer.
Tudorizer
If you know it is wrong, then why did you post it as an answer?
patrick dw
+2  A: 

Try this:

$(":selected", this)

This limits the ":selected" selector to the context of the "this" object. More info here.

Tim S. Van Haren
+3  A: 

You can specify this as the context of the selector:

$(":selected",this)

But it ultimately just gets turned around into @Gumbo's answer. So that would be a little more efficient.


Ultimately, I wouldn't use either. If you actually need to selected <option>, this would be another approach:

this.options[ this.selectedIndex ].text.indexOf(...;

Since this is the <select> element, you use its options property which stores an Array of the options, and its selectedIndex property to get the selected one from the options.

Then you use the <option> element's text property to get the text.

More efficient this way.

patrick dw
Hey Patrick, thanks for the edits and explanations.
@user - You're welcome. :o)
patrick dw
A: 

The jQuery 'is' selector seems to do what you want although I haven't checked it: http://api.jquery.com/is/

if($(this).is(":selected").text().indexOf("[Subtract") >= 0){ ... }
Jim
This is incorrect. `.is()` returns a boolean.
patrick dw
-1 `is` returns boolean.
Gumbo