Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Pass variable to :eq() does not work

I have been trying to find out why the following lines of code do not work:

$('#add-cloud > select').change(function() {
    var selected = parseInt($('#add-cloud select option:selected').val()); 
    $("#cloud-calculator table tr:eq(selected)").css("color", "red");
});

If I change :eq(selected) to :eq(4) for example - works fine. How do you pass variable as an argument to :eq() ?

like image 557
dalizard Avatar asked Jul 02 '09 14:07

dalizard


3 Answers

You have to concatenate your variable with your selector:

$("tr:eq("+selected+")");
like image 199
Sampson Avatar answered Nov 07 '22 03:11

Sampson


The way you're doing it, you're embedding the actual string "selected" in the selector. You need to construct a string using your selected variable as a part of it:

$("#cloud-calculator table tr:eq(" + selected + ")").css("color", "red");
like image 4
chaos Avatar answered Nov 07 '22 02:11

chaos


Also, you can simply use the 'this' object to get the seleted value.

$('#add-cloud > select').change(function() 
{
    var rowSelector = '#cloud-calculator table tr:eq(' + parseInt(this.val()) + ')';
    $(rowSelector).css("color", "red");
}
like image 1
SolutionYogi Avatar answered Nov 07 '22 03:11

SolutionYogi