javascript - Find DOM element by ID when ID contains square brackets? -
i have dom element id similar to:
something[500]
which built ruby on rails application. need able element via jquery can traverse way dom delete parent of it's parent, has variable id don't have access beforehand.
does know how go this? following code doesn't seem working:
alert($("#something["+id+"]").parent().parent().attr("id"));
upon further inspection, following:
$("#something["+id+"]")
returns object, when run ".html()" or ".text()" on it, result null or empty string.
any appreciated.
you need escape square brackets not counted attribute selectors. try this:
alert($("#something\\["+id+"\\]").parent().parent().attr("id"));
see special characters in selectors, second paragraph:
to use of meta-characters (such
!"#$%&'()*+,./:;<=>?@[\]^``{|}~
) literal part of name, must escaped with 2 backslashes:\\
. example, elementid="foo.bar"
, can use selector$("#foo\\.bar")
. w3c css specification contains complete set of rules regarding valid css selectors. useful blog entry mathias bynens on css character escape sequences identifiers.
Comments
Post a Comment