Here is my jquery:
if ($('div[data-sort= 23 ]').length > 0) {
$('div[data-sort= 23 ]').after(newdiv);
}
If I want to set the number 23 equal to n this is what I’ve done:
n = 23;
if ($('div[data-sort= n ]').length > 0) {
$('div[data-sort= n ]').after(newdiv);
}
But this doesn’t work. Can anyone tell me why?
Try:
You need to make your variable part of the string; in this new code the number
23is concatenated into the string so the selector for jQuery will work. To see what jQuery receives as the selector you could run this:You should see “div[data-sort=23]”.