-1

I am using the following to access specific DOM elements with the placeholder "Serial"

$('input[placeholder=Serial]')

And it's working fine. I have some other DOM elements having placeholder of "@SAL", when I try to access them using:

$('input[placeholder=@SAL]')

It throws this error

Error: Syntax error, unrecognized expression: input[placeholder=@SAL]

But when I try to access the same using double quotes " around @SAL it's working fine.

Now the question is, why does $('input[name=Serial]') work fine i.e. without the double quotes around the placeholder value but $('input[placeholder=@SAL]') throw the above said error?

P.S I know it's a bad idea to access DOM elements based upon their placeholders (as they may change), but I was just curious that why is it not working in this specific case...

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101
  • 2
    @ Kamran: I expect it's probably lack of research effort. This information is *very* easy to find -- it's in the jQuery docs, the CSS selectors docs, on MDN... – T.J. Crowder Apr 29 '14 at 13:06
  • Does this answer your question? [CSS attribute selectors: The rules on quotes (", ' or none?)](https://stackoverflow.com/questions/5578845/css-attribute-selectors-the-rules-on-quotes-or-none) – Heretic Monkey Jan 26 '22 at 14:24

3 Answers3

7

It's right there in the docs -

Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.

http://api.jquery.com/category/selectors/attribute-selectors/

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

Note that Unicode is code-by-code equivalent to ISO 10646 (see [UNICODE] and [ISO10646]).

http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier

A selector that begins with the '@' is not a valid selector but can be remedied by using quotes or by escaping the characters in question.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

Because @ is a special character and this either needs to be escaped like this:

$('input[placeholder=\\@SAL]')

(The actual selector has one backslash, but because this is a JavaScript string literal, we have to use \\ to get a single backslash into the selector.)

or select it with string value with quotes like this:

$('input[placeholder="@SAL"]') // this does not need to escaped.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Jai
  • 74,255
  • 12
  • 74
  • 103
0

The only difference between your two situations is the @ symbol, therefore you should either escape it or I would quote it to make it a string. It should most definitely work that way.

cpuxtech
  • 9
  • 1