Iladarsda
Iladarsda

Reputation: 10696

jQuery selector unintelligible issue

I have following example

What it should do:

And in the last step it is failing as you can see. console.log( $('this').parent('li') ); = []

Why? What is wrong? Any suggestion much appreciated.

Upvotes: 2

Views: 48

Answers (2)

karim79
karim79

Reputation: 342635

this is an DOM object, not a selector string, so you need:

$(this).parent('li').addClass('selected');
console.log($(this).parent('li'));

instead of:

$('this').parent('li').addClass('selected');
console.log($('this').parent('li'));

$('this') will cause jQuery to construct an object which wraps all elements matching your selector. 'this' is not a valid selector, so you get that 'selector unintelligible' error, whereas $(this) refers to the jQuery-wrapped clicked anchor.

Upvotes: 6

Evan
Evan

Reputation: 6115

not sure what box wrapper has to do with it but this click function should work:

$(".tabs li a").click(function(){
     $(".tabs li").removeClass("selected");
     $(this).parent().addClass("selected");
}

Upvotes: 1

Related Questions