I have div's with ids that are pulled from a database. These ids sometimes contain parentheses and this causes the JQuery selector to not work. What can I do?
Here is an example of what I am talking about:
var element = 'hello (world)'; $('#' + element).hide();
You can use the attribute selector for ID
$("[id='" + element + "']").hide();
or modify your string selector with a regex to remove the parentheses and spaces
element = element.replace(/(?=[() ])/g, '\\'); $('#' + element).hide();
Rather than using an attribute selector, as the other answer suggests, I would suggest switching back to the native
getElementById, which takes a literal id and not a selector, the DOM scan will not happen.
©2020 All rights reserved.