How to put 2 elements concatenated with variable in a selector with jQuery?

2

I have 2 variables and I want to put the same concatenated ones in just one selector with jQuery:

I know you can do this:

$(".classe1, .classe2").append(...);

But I want to concatenate these two classes with a variable and put the two inside a selector more or less like this (this example is giving error):

var to = $(".meunome").attr("id");
var from = $(".amigonome").attr("id");

$(".classe1'+to+', .classe2'+from+'").append(...);
    
asked by anonymous 02.05.2015 / 22:36

1 answer

3

You got the id, so you have to use '#' as a selector:

var to = $(".meunome").attr("id");
var from = $(".amigonome").attr("id");

$('#' + to + ', #' + from).append(...);
    
02.05.2015 / 22:43