1

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
rball
  • 6,925
  • 7
  • 49
  • 77

6 Answers6

10

Example page: http://jsbin.com/idali (view/edit source at http://jsbin.com/idali/edit)

The following variations should all work:

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

(edit: the original answer below is wrong; quotes are optional but allowed...)

You've got too much quoting in your attempts... You want

$("span[id$=Me],input[id$=Me]")

Attribute values are not quoted in css selectors.

Stobor
  • 44,246
  • 6
  • 66
  • 69
  • $("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 Nov 18 '09 at 00:14
  • Unexpected call to method or property access. – rball Nov 18 '09 at 00:23
  • 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 Nov 18 '09 at 00:25
  • 1
    The error I'm seeing isn't the selector, it's trying to set the text of both of them later. – rball Nov 18 '09 at 00:29
  • @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 Nov 18 '09 at 02:03
4

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
  • 44,698
  • 7
  • 80
  • 103
2
$('span[id$=Me]').add('input[id$=Me]')
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
0
$('input#iMe, span#sMe');
eKek0
  • 23,005
  • 25
  • 91
  • 119
0
$("[id$='Me']")

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

Chetan S
  • 23,637
  • 2
  • 63
  • 78
  • Tried that, it literally froze the browser. Also tried *[id$=Me]. – rball Nov 18 '09 at 00:18
  • Looks like your DOM is too complex. Try to descend from a parent with an id selector, something like - `$("#parentDiv").find("[id$='Me']")` – Chetan S Nov 18 '09 at 00:31
  • Few tips http://stackoverflow.com/questions/46214/good-ways-to-improve-jquery-selector-performance http://www.artzstudio.com/2009/04/jquery-performance-rules/ – Chetan S Nov 18 '09 at 00:31
0
$("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?