The first example $('div[name=name[1]]');
is incorrect, and gives the error
unrecognized expression: div[name=name[1]]
.
The other options are correct, although for slightly different reasons.
Explanation:
$('div[name="name[1]"]')
it’s okay to use because jQuery treatsname[1]
as a string, since it is inside quotes, and not as a CSS/jQuery selector, and so does not need to be shielded with the exhaust \\
.
$('div[name="name\\[1\\]"]')
, works, but does not need to \\
. jQuery reads the selector name\\[1\\]
as a string because it is inside quotes, and in javascript the backslash causes the second bar to be ignored\
resulting in \[
, which for your sake is the same as [
. So this example has inverted bars without need.
$('div[name=name\\[1\\]]')
it’s okay and internal right relatives []
have to be shielded with backslashes so as not to be confused with CSS/jQuery selectors.
From the jQuery documentation:
To use any of the following metacharacters !"#$%&'()*+,./:;<=>?@[]^`{|}~
as part of a name, they must be shielded with two inverted bars, bars on the left: \\.
More reading (in English):
It’s fun to learn Portuguese here :) Square Bracket.
– Maniero
Now I was in doubt, straight relative is not correct (or correct :) in Portugal?
– Maniero
@bigown, I think that after last year’s orthographic agreement straight went straight... but since I’ve been living in the South for 5 years I still speak Portuguese "old" :)
– Sergio
"square bracket"
– Alexandre Cartaxo
@Alexandrecartaxo then must be with "i" in parenthesesis? and should be "straight" or "straight"?
– Sergio
@Sergio "parentheses" and "parentheses" are synonymous. I use the spelling "straight" because I don’t write according to the spelling disagreement. By the way, it is "there" (from the verb "to be" and not "to", which is a contraction of a preposition with a definite article).
– Alexandre Cartaxo
@Alexandrecartaxo ok :) an Edit is welcome!
– Sergio
@Sergio Not necessarily. As per AO90 is correct.
– Alexandre Cartaxo