1
$('a[href=#InterventionEditDocs]').trigger('click');

This line of code was working for my project perfectly a few days back but after some updates or something it throws an error in the console.

I fixed it by adding quotes around the value:

$('a[href="#InterventionEditDocs"]').trigger('click');

and it is now working. But why was it working before, and why do I have to quote the value for it to work again?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
rajiv
  • 64
  • 1
  • 6
  • [`To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id="foo.bar", can use the selector $("#foo\\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors. Also useful is the blog entry by Mathias Bynens on CSS character escape sequences for identifiers.`](https://api.jquery.com/category/selectors/) – Pranav C Balan Feb 01 '17 at 06:28
  • `$('a[href=\\#InterventionEditDocs]').trigger('click');` – Pranav C Balan Feb 01 '17 at 06:29
  • also, your question title mentions `[attribute$=value]` yet the question itself only uses `[attribute=value]` – Antti Haapala -- Слава Україні Feb 01 '17 at 06:29
  • You must have updated jQuery library – Satpal Feb 01 '17 at 06:30

1 Answers1

3

But why was it working before

This was a bug with jQuery that was fixed in 1.12.0 and 2.2.0.

a[href=#InterventionEditDocs] is an invalid CSS selector, because # is a special character (representing an ID selector), and therefore cannot appear in an ident. It never should have worked in jQuery in the first place, and would have resulted in a SYNTAX_ERR if put through document.querySelectorAll().

and why do I have to quote the value for it to work again?

a[href="#InterventionEditDocs"] is valid, because the attribute value is now quoted, and therefore no longer an ident but a string.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356