qadenza
qadenza

Reputation: 9293

variable is not visible in eq selector

$('#ulLeft li').click(function () {
x = $(this).index();
change();
});  

function change(){
$('.cAct').removeClass().addClass('cPass');
alert (x);  //works fine
$('#ulLeft li:eq(x)').removeClass().addClass('cAct');  //doesn't work
}

In the last line x is not visible.
Changing x with some integer - works.
Any suggestion ?

Upvotes: 0

Views: 76

Answers (3)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You have to concatenate that variable x with the string to achieve what you needed.

Try,

$('#ulLeft li:eq('+ x +')').removeClass().addClass('cAct'); 

Or use .eq(index) to do it with out concatenations.

$('#ulLeft li').eq(x).removeClass().addClass('cAct'); 

Upvotes: 1

Satpal
Satpal

Reputation: 133423

Use $('#ulLeft li:eq('+x+')') instead of $('#ulLeft li:eq(x)')

As x is a variable you need to use string concatenation

Or use .eq()

$('#ulLeft li').eq(x)

Upvotes: 1

karthikr
karthikr

Reputation: 99660

Try this:

$('#ulLeft li:eq(' + x + ')').removeClass().addClass('cAct');

Since x is a variable, you need to check :eq against the value assigned to the variable.

Upvotes: 2

Related Questions