0

I want to pass a variable in a selector jquery but I don't find the right syntaxe.

I have this :

var jsonobject = eval(responseObject);

    for(var item in jsonobject)
    {
        $('#",jsonobject[item],"').css("background-color","red");                   
    }

So the variable is jsonobject[item] and I want to use the value in it as the name of the selector.

Thanks for your help !

Erlaunis
  • 1,353
  • 5
  • 29
  • 48

2 Answers2

5

You need to use

$("#"+jsonobject[item]).css("background-color","red");  
Milind Anantwar
  • 79,642
  • 23
  • 92
  • 120
1

Simply use string concatenation for that:

$('#' + jsonobject[item]).css(...);
Justinas
  • 37,569
  • 4
  • 61
  • 88