1

Why selector works in first example, but fails in second one? See jsfiddle.

<div id="hello[1][2]_world">&nbsp;</div>
<textarea id="console"></textarea>

<script>
   $(document).ready(function() {

      //first example
      $('#hello\\[1\\]\\[2\\]_world').html('01234'); //everything is OK

      //second example
      var iid = 'hello[1][2]_world';    
      iid = iid.replace(/[#;&,.+*~':"!^$[\]()=>|\/]/g, "\\\\$&");
      $('#console').val(iid); //see in textarea, the same string as from first    

      $('#'+iid).html('56789'); //not working! whyyyyyyyy? :)

   });    
</script>
Mat
  • 188,820
  • 38
  • 367
  • 383
Kirzilla
  • 15,130
  • 23
  • 80
  • 126

4 Answers4

3

The first string is "actually" '#hello\[1\]\[2\]_world'.

What happens is that you have to escape \s in the first case so that the $() function receives \[ and so on so that it knows that it should treat those characters as part of the id.

In the second case you're creating a string that's hello\\\\[1\\\\]\\\\[2\\\\]_world (as you have 4 backslashes in the replacement!) that becomes, after the backslashes have been escaped, hello\\[1\\]\\[2\\]_world.

You can confirm this by adding these lines at the end (look in the JS console):

console.log( iid );
console.log( 'hello\\[1\\]\\[2\\]_world' );

http://jsfiddle.net/qSpaK/7/

JJJ
  • 31,545
  • 20
  • 84
  • 99
1

jQuery's FAQ has a nice solution for this that escapes all character that jQuery requires.

I like this nullsafe version of what they suggest:

function jqid (id) {
  return (!id) ? null : '#' + id.replace(/(:|\.|\[|\]|,)/g, '\\$1');
}

See answers to these questions also:

jQuery selector value escaping

Need to escape a special character in a jQuery selector string

Community
  • 1
  • 1
theUtherSide
  • 2,800
  • 3
  • 28
  • 34
1

You don't need to double escape the second one:

<div id="hello[1][2]_world">&nbsp;</div>
<textarea id="console"></textarea>

<script>
   $(document).ready(function() {

      //first example
      $('#hello\\[1\\]\\[2\\]_world').html('01234'); //everything is OK

      //second example
      var iid = 'hello[1][2]_world';    
    iid = iid.replace(/[#;&,.+*~':"!^$[\]()=>|\/]/g, "\\$&");
      $('#console').val(iid); //see in textarea, the same string as from first    

      $('#'+iid).html('56789'); //not working! whyyyyyyyy? :)
   });    
</script>

Because really \[ is that symbol escaped. But in your first string you have to escape the \\ and in the second you are doing this \\\\. If that makes sense.

Joe
  • 73,764
  • 18
  • 123
  • 142
0

That's not a valid ID, and as thus you're relying on undefined behaviour. See this question. Can I suggest using data attributes for storing arbitrary data instead of doing it in the ID?

Community
  • 1
  • 1
Alex Turpin
  • 43,483
  • 22
  • 107
  • 143
  • I've tried to use `rel` attribute or any other, but the same problem, see http://jsfiddle.net/FJpvz/2/ – Kirzilla Nov 22 '11 at 18:01
  • 1
    You don't need to escape the brackets when they're not used as a selector, see http://jsfiddle.net/FJpvz/3/ – JJJ Nov 22 '11 at 18:04