MSTC
MSTC

Reputation: 29

Concatenation in a jQuery selector

I have a problem with this selector, I don't know why it isn't working, it's driving me nuts.

I have this:

variable = "#2";

$('"'+variable+'"').css("background-color", "red"); 

https://i.sstatic.net/HgqYJ.png

Can you help me?

Thank you in advance!

Upvotes: 1

Views: 42

Answers (1)

Tushar
Tushar

Reputation: 87203

You don't need concatenation in this case. You can use variable directly as selector.

$(variable).css("background-color", "red");
  1. Use the variable as it is in the jQuery as selector
  2. Change the name of the variable to something else as javascript has it as reserved keyword.

Upvotes: 1

Related Questions