I have a variable that I want to use in a selector but the variable contains special characters. Is there a way to do this?
hasSpecialChr='dynamicVar[1][3]';
element = $('li.parent#'+hasSpecialChr);
Edit: Answered in first comment below.
I have a variable that I want to use in a selector but the variable contains special characters. Is there a way to do this?
hasSpecialChr='dynamicVar[1][3]';
element = $('li.parent#'+hasSpecialChr);
Edit: Answered in first comment below.
Try this:
hasSpecialChr='dynamicVar\\[1\\]\\[3\\]';
element = $('li.parent#'+hasSpecialChr);
This may solve your problem:
s.replace(/[^a-z\d\s]+/gi, "");
removes all except letters, numbers and white space.
hasSpecialChr='dynamicVar[1][3]';
hasSpecialChr.replace(/[^a-z\d\s]+/gi, "");
element = $('li.parent#'+hasSpecialChr);