Find DOM element by ID when ID contains square brackets?

JavascriptJqueryRuby on-RailsDom

Javascript Problem Overview


I have a DOM element with an ID similar to:

something[500]

which was built by my Ruby on Rails application. I need to be able to get this element via jQuery so that I can traverse my way up the DOM to delete the parent of it's parent, which has a variable ID that I don't have access to beforehand.

Does anyone know how I could go about this? The following code doesn't seem to be working:

alert($("#something["+id+"]").parent().parent().attr("id"));

Upon further inspection, the following:

$("#something["+id+"]")

returns an object, but when I run ".html()" or ".text()" on it, the result is always null or just an empty string.

Javascript Solutions


Solution 1 - Javascript

You need to escape the square brackets so that they are not counted as attribute selectors. Try this:

alert($("#something\\["+id+"\\]").parent().parent().attr("id"));

See Special Characters In Selectors, specifically the second paragraph:

>To use any of the meta-characters (such as !"#$%&'()*+,./:;<=>?@[\]^``{|}~) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.

Solution 2 - Javascript

You can also do

$('[id="something['+id+']"]')

Solution 3 - Javascript

Square brackets have special meaning to jQuery selectors, the attribute filters specifically.

Just escape these and it will find your element fine

$( "#something\\[" + id + "\\]" )

Solution 4 - Javascript

An id cannot include square brackets. It is forbidden by the spec.

Some browsers might error correct and cope, but you should fix you data instead of trying to deal with bad data.

Solution 5 - Javascript

Try this:

alert($("#something\\["+id+"\\]").parent()[0].parent()[0].attr("id"));

Solution 6 - Javascript

You can escape them using \\ or you could do something like this...

$(document.getElementById("something[" + id + "]"))

Solution 7 - Javascript

"Any of the meta-characters

!"#$%&'()*+,./:;<=>?@[\]^`{|}~

as a literal part of a name, it must be escaped with with two backslashes: \\.

For example, an element with id="foo.bar", can use the selector

$("#foo\\.bar")

" [source: jquery doc], and an element with id="foo[bar]" (even though not valid for W3C but recognised by JQuery), can use the selector

$("#foo\\[bar\\]")

(Just an asnwer like the many others, but all together :))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike TrpcicView Question on Stackoverflow
Solution 1 - Javascriptkarim79View Answer on Stackoverflow
Solution 2 - JavascriptsteampoweredView Answer on Stackoverflow
Solution 3 - JavascriptPeter BaileyView Answer on Stackoverflow
Solution 4 - JavascriptQuentinView Answer on Stackoverflow
Solution 5 - JavascriptTim S. Van HarenView Answer on Stackoverflow
Solution 6 - JavascriptJosh StodolaView Answer on Stackoverflow
Solution 7 - JavascriptE CiottiView Answer on Stackoverflow