Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an element inside "this" in jQuery?

Tags:

jquery

I know can I select an element this way:

$("ul.topnav > li.target").css("border", "3px double red"); 

but how can I do something like:

$(this > li.target).css("border", "3px double red"); 
like image 806
deb Avatar asked Feb 01 '11 22:02

deb


People also ask

What is $( this in jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.

How do I select elements when I already have a DOM element?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.

How do you select an element with a particular class Sselected?

How to select element having a particular class (". selected")? Ans: $('. selected').


2 Answers

$( this ).find( 'li.target' ).css("border", "3px double red"); 

or

$( this ).children( 'li.target' ).css("border", "3px double red"); 

Use children for immediate descendants, or find for deeper elements.

like image 100
hookedonwinter Avatar answered Oct 27 '22 01:10

hookedonwinter


I use this to get the Parent, similarly for child

$( this ).children( 'li.target' ).css("border", "3px double red"); 

Good Luck

like image 31
mchinta Avatar answered Oct 27 '22 01:10

mchinta