views:

466

answers:

6

I have two elements that I would like to select

<input id="iMe" /> and <span id="sMe">Blah</span>

I would like to select them both:

$("span[id$='Me']") and $("input[id$='Me']")

in one selector. I've tried:

$("span,input[id$='Me']") -> Nope
$("span[id$='Me'],input[id$='Me']") -> Nope
$("span[id$='Me']input[id$='Me']") -> Nope

I wouldn't mind just adding it to the collection either. I definitely don't want to create more script to hack around this. Any ideas?

+2  A: 
$('span[id$=Me]').add('input[id$=Me]')
Scott Evernden
Doesn't seem to work. Shoot.
rball
Tried $("span[id$='Me']").add("input[id$='Me']"); threw a js error.
rball
@rball: get rid of the quotes in 'Me'...
Stobor
I tried that first, same result. I'll retry one more time.
rball
Full line: $component = $("span[id$=Me]").add("input[id$=Me]"); no dice
rball
my answer and $('span[id$=Me], input[id$=Me]') both DO work with your example code. you're leaving something out of your question
Scott Evernden
are you maybe forgetting to include the jquery script library ?
Scott Evernden
+2  A: 

Why not give the two elements a class?

<input class="frobbable" id="iMe" /> and <span class="frobbable" id="sMe">Blah</span>

then

$(".frobbable")

?

Jonathan Feinberg
That's a good idea
rball
Crap, the asp.net hidden field doesn't have a cssClass property. Yah! :(
rball
Well, I don't know what asp.net is, but if it's something that hides HTML from you, it's broken by design.
Jonathan Feinberg
A: 
$('input#iMe, span#sMe');
eKek0
asp.net so I'm matching the ends, that's why I have an attribute selector
rball
+3  A: 
Stobor
$("span[id$=Me],input[id$=Me]"); throwing an error...how wierd. I believe you, I'm just confused why the heck it's throwing the error.
rball
$("span[id$=Me]"); with or without quotes is working.
rball
What error is it throwing?
Stobor
Unexpected call to method or property access.
rball
Also just a FYI: From jQuery docs Quotes are optional in most cases, but should be used to avoid conflicts when the value contains characters like "]". So i don't think the quotes are doing it.
rball
The error I'm seeing isn't the selector, it's trying to set the text of both of them later.
rball
@rball: Yeah, I read the docs for quotes shortly before you pointed that out... I've never used them before, but I can see where they might be needed. As to your issue, if you're trying to set the text using the `$.text()` method, that might be your problem; I think (haven't checked) that you're supposed to use `$.value()` for text-input controls...
Stobor
A: 
$("[id$='Me']")

should work. But watch out for the performance as this will have to go through the entire DOM.

Chetan Sastry
Tried that, it literally froze the browser. Also tried *[id$=Me].
rball
Looks like your DOM is too complex. Try to descend from a parent with an id selector, something like - `$("#parentDiv").find("[id$='Me']")`
Chetan Sastry
Few tips http://stackoverflow.com/questions/46214/good-ways-to-improve-jquery-selector-performance http://www.artzstudio.com/2009/04/jquery-performance-rules/
Chetan Sastry
A: 
$("span[id$='Me'],input[id$='Me']") -> Yep

This really should work as well as the add() method, so maybe you have a problem somewhere else? Some ancient version of jQuery perhaps?

architech