Search code examples
jqueryjdeveloper

jquery live and : problem


$('#ptadsd1:r1:0:cbN89').live('click', function(event1) { 
    alert('clicked'); 
    return false; 
});

does not work becouse jdeveloper uses ":" when creating IDs.

my id is "ptadsd1:r1:0:cbN89" for example. Jdeveloper use ":" when creating ids but jquery has problem with it. How can i solve the problem with ":"?


Solution

  • You need to escape the : with a \. But to prevent JavaScript from treating the \: as a single character, you need to double escape it:

    $('#ptadsd1\\:r1\\:0\\:cbN89');
    

    Relevant reference:

    If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;?@[\]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an element with id="foo.bar", you can use the selector $("#foo\\.bar").

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