Question

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?

Was it helpful?

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();

OTHER TIPS

I've probably misunderstood the question - but, would this work?

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

can't you do something like $("#"+imgId, ".img").remove()

Use .replace to replace it.

var imgId = this.id.replace('.', '/.'); //Or whatever other escaping is there.

While the period is a valid entity in an HTML Id I strongly sugest you follow a different naming convention, specifically one where periods are not used in Id values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top