How to add a variable inside jQuery selector

good afternoon

I want to add a variable to the jQuery selector, but the syntax is incorrect:

var c = $(this).attr('class'); if ($('#articleThumb ul li img').hasClass(c)) { $('#articleThumb ul li img.'+c').clone().appendTo('#articleFeatured'); } 

the class

img.class is a variable, not a constant, like img.birds .

thanks

+4
source share
3 answers

Your quotes are disabled, pay attention to the wandering after c . It should be:

 $('#articleThumb ul li img.' + c) 
+4
source

Looks like you're adding an extra '

 $('#articleThumb ul li img.' + c) 
+2
source

You can simply combine the variable with the selector string. Remove the extra single quote at the end of c in the selector.

Change

  $('#articleThumb ul li img.'+c') 

For

 $('#articleThumb ul li img.' + c) 
+1
source

All Articles