jQuery period in class name stored in var
-
29-10-2019 - |
سؤال
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?
المحلول
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();
نصائح أخرى
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.
لا تنتمي إلى StackOverflow