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, element id="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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -