Search code examples
jqueryclass-names

jQuery period in class name stored in var


I have this code:

var imgId = this.id; //file1.jpg

and this:

$('.img\\' + imgId).remove();

So the problem is, i cant escape the '.' because its in the var, not before. What can i do?


Solution

  • Don't store the filename as part of the class/id in your markup. Instead, use a data- attribute:

    <div class="img" data-filename="file1.jpg" />
    

    And then in your jQuery:

    $('.img[data-filename="'+imgId+'"]').remove();