Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select elements by attribute's value when values contains dollar sign?

I have the following HTML element:

<input type="checkbox" name="ctl00$ContentPH$ucFuncionEdit1$ckEsMenu" />

How can I select all elements with the same name using jQuery, the following fails:

jQuery('[name=ctl00$ContentPH$ucFuncionEdit1$ckEsMenu]');

The previous line of code raise the following error:

Error: Syntax error, unrecognized expression: [name=ctl00$$ContentPH$$ucFuncionEdit1$$ckEsMenu]

like image 418
Rubens Mariuzzo Avatar asked Feb 20 '12 14:02

Rubens Mariuzzo


People also ask

How do I find an element by its attribute?

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value. The following example selects all elements with a class attribute value that starts with "top": Note: The value does not have to be a whole word!

When working with attribute selectors how can you select elements which contain a particular attribute value?

[attribute$=”value”] Selector: This selector is used to select all the elements whose attribute value ends with the specified value. The value doesn't need to be a whole word. [attribute*=”value”] Selector: This selector selects all the elements whose attribute value contains the specified value present anywhere.

What does dollar sign do in HTML?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

Which CSS rule will select an element with that used the alt attribute on the image element?

To use this selector, add a tilde (~) before the equals sign. For example, img[alt~="art"] will select images with the alt text “abstract art” and “art show”, but not “athlete starting a new sport” (which the “contains” selector would select). Value starts with: attribute value starts with the selected term.


2 Answers

You quote the value:

jQuery ('[name="ctl00$ContentPH$ucFuncionEdit1$ckEsMenu"]');

When dealing with attribute selectors, it's best to always quote the value (although if the value is a single word containing only the letters A-Z [case insensitive] and the digits 0-9 [but not starting with a digit], you can get away without).

like image 73
T.J. Crowder Avatar answered Oct 19 '22 11:10

T.J. Crowder


You can escape the $ sign with with two backslashes (\\):

jQuery('[name=ctl00\\$ContentPH\\$ucFuncionEdit1\\$ckEsMenu]');

like image 26
Ivan Korshun Avatar answered Oct 19 '22 12:10

Ivan Korshun