Reputation: 3046
I have to select a div
element id
with escape characters
,
HTML
<div id="abc/def"></div>
Tried these,
$("#abc/def").find('h4').attr('data-val', "test") //didn't work
$("id=[abc/def]").find('h4').attr('data-val', "test") //didn't work
$("#"+escapeSelector("abc/def")).find('h4').attr('data-val',"Test") //didn't work
function escapeSelector(s) {
return s.replace(/(:|\.|\[|\])/g, "\\$1");
}
Error:
Syntax error, unrecognized expression: #abc/def
Where should i make the change to make this work?
Upvotes: 1
Views: 4439
Reputation: 1962
$("#abc\\/def").text("test") //did work :)
https://jsfiddle.net/gtpkkyw0/
There is really good info on the jquery site as well. Leaving here as a reference, hope it helps someone:
https://api.jquery.com/category/selectors/
Upvotes: 3
Reputation: 8189
You don't have to escape anything with the Attribute equals selector :
$('[id="abc/def"]')
https://jsfiddle.net/86uohuny/
Upvotes: 2