1

I was a bit confused when I encountered this in my programm. So I was wondering if anyone knew what could be the deal here. I am fairly new to Jquery after all.

Why does:

$('[id="' + nameOftheID + '"]').append("Append some text");

work for me.

Yet

$("#" + nameOftheID).append("Append some text");

does not work for me. In this case the 'nameOftheID' is a var that is exactly the same as the ID of the span I am trying to append some text to (I tested and confirmed this in debugging). Why is it that the normal # selector for selection based on ID's does not append the text for me. but the other method does?

My code works in the end. I just want to know why A works and B doesnt.

The name of my ID is "Ruleset[0].Conditions[0]"

Leonard
  • 47
  • 8

4 Answers4

2

You have to use \\ for escape characters

#Ruleset\\[0\\]\.Conditions\\[0\\]

Below is the differnce for two selectors :

$("#" + nameOftheID).append("Append som text");

Above code is to select element by ID and here syntax is to use #.

And

$('[id="' + nameOftheID + '"]').append("Append some text");

This comes under attribute selector, where you can restrict the selection for specific tag like below

$('input[id="inputId"]').append("Append some text");// here it will check the id attribute of input tag only

Also, attribute selectors can be use to find elements with value start with some text like find input with id start with 'nameOf'

$('input[id^="nameOf"]').append("Append some text");

Hence attribute selectors can be used for get elements with more filtering or for more precise selections. Please find more on selectors here.

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
1

The ID is Ruleset[0].Conditions[0] as an example

Your ID contains characters with special meaning in a selector.

If you just prefix that with a # then the [ will start an attribute selector and the . will start a class selector. You have to escape characters with special meaning in a selector if you use them in an id selector.

#Ruleset\[0\]\.Conditions\[0\]

Note that you must include the selector escape characters in the string and that \ is an escape character for JavaScript string literals (which will require escaping itself in order to make \ part of the string).

Those characters don't have special meaning inside a quoted attribute selector.

I prefer to avoid the problem by keeping characters with special meaning out of id values in the first place.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

My code works in the end. I just want to know why A works and B doesnt.

Sizzle (the part of jQuery that handles $(...) things) assumes an ID to be the following:

"(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+"

That is, an escaped character, or a word char, or an ASCII char higher than \xA0. That excludes all punctuation and regex metacharacters like [. So when you something like #foo[] to the $ function, it won't be recognized and throw the error:

Uncaught Error: Syntax error, unrecognized expression: #foo[] 

The attribute selector, on the other side, only requires a quoted string and doesn't care about its content.

gog
  • 10,367
  • 2
  • 24
  • 38
0

As i see in the comments section that your span's id holds some special chars like [] brackets:

This code

$('[id="' + nameOftheID + '"]')

represents like this:

$('[id="Ruleset[0].Conditions[0]"]') // works because of string representation.

This represents the id as a string so it works but for the other one this is not the case, for that id representation you have to escape your special chars with double slashes \\

Jai
  • 74,255
  • 12
  • 74
  • 103