Pass a variable in a selector jquery [duplicate] - javascript

This question already has answers here:
jQuery selectors with variables
(5 answers)
jQuery: using a variable as a selector [duplicate]
(1 answer)
Closed 7 years ago.
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 !

You need to use
$("#"+jsonobject[item]).css("background-color","red");

Simply use string concatenation for that:
$('#' + jsonobject[item]).css(...);

Related

Calling Javascript variable inside javascript code [duplicate]

This question already has answers here:
How can I do string interpolation in JavaScript?
(21 answers)
Closed 6 years ago.
I have the following code
dt.ajax.url( 'test.php?status=' ).load();
I defined a variable
var status= 55
I wand to add status_2 in the link like this
dt.ajax.url( 'test.php?status=&status' ).load();
How can I do it ?
Try this. You need to concatenate the variable with url
dt.ajax.url('test.php?status='+status).load();

Event listener for multiple elements, when stored as variables [duplicate]

This question already has answers here:
How do I store multiple jQuery selectors in a javascript variable?
(5 answers)
Closed 6 years ago.
Is there a way to execute the same code for multiple elements when they are stored as variables in Jquery?
For example:
$('.class1, .class2').on('click', some_function);
when stored as variables:
var class1 = $('.class1),
class2 = $('.class2);
$class1, $class2.on('click', some_function);
add method to the rescue!
$class1.add($class2).on('click', some_function);

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Calling a jQuery function using a variable name [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
I have the following code where MODE is a variable. Its value should be a method of jQuery's tinycolor liberary.
$cor = tinycolor($cor).mode(z).toString();
I'd like to call that method on that line so, for instance, when mode = 'lighten' $cor would be
$cor = tinycolor($cor).lighten(z).toString();
Is there a way of doing it this way?
Thanks!
Use bracket notation
$cor = tinycolor($cor)[mode](z).toString();

Append variable value to form Input [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
I need to append numeric value stored in one variable to form input fields.
Let's Say
var b= 5;
var text=$("#dropdown_id").val();
I want to append value of variable b in dropdown_id .
My expected result is #dropdown_id5
Use string concatenation:
$("#dropdown_id" + b).val()

Categories