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,433
  • 6
  • 32
  • 50

2 Answers2

5

You need to use

$("#"+jsonobject[item]).css("background-color","red");  
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Simply use string concatenation for that:

$('#' + jsonobject[item]).css(...);
Justinas
  • 41,402
  • 5
  • 66
  • 96