Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference an element of $(this) in jQuery

I'm iterating through a series of elements in jQuery (intentionally).

So basically I want something like this, but with the correct syntax:

$(".sonicrow").each(function() {
    $(this + 'somediv').css('background-color', 'red');
});

Obviously this is some kind of object/string mishmash. What would the correct syntax be for accessing the specific somediv within the this object?

Thanks, John.

like image 997
John Hunt Avatar asked Feb 25 '23 11:02

John Hunt


1 Answers

$(".sonicrow").each(function() {
    $('somediv', this).css('background-color', 'red');
});

Where the second parameter is the "context" of the selector. Of cause your somediv have to be .somediv if it´s a class or #somediv if it´s an id.

This question is related to How to get the children of the $(this) selector? which also contains this answer

...
    $(this).find('somediv').css(...)
...

According to jQuery context selector $(selector, context) is implemented with $(context).find(selector).

like image 95
Lasse Espeholt Avatar answered Mar 08 '23 15:03

Lasse Espeholt