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");
$(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.
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 to select element having a particular class (". selected")? Ans: $('. selected').
$( 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.
I use this to get the Parent, similarly for child
$( this ).children( 'li.target' ).css("border", "3px double red");
Good Luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With