Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery this concat selector

For example given this HTML:

<div>
    <p>p0</p>
    <p>p1</p>
    <p>p2</p>
</div>

I don't understand how to write a concatenated jQuery this selector, that is something like this:

$("div").on("mouseover", function () {
    $(this + " p").css({
        color: "#009"
    });
}); 

What is the correct syntax for this?

like image 201
Oto Shavadze Avatar asked Nov 05 '12 16:11

Oto Shavadze


People also ask

What is concat jQuery?

The concat() method joins two or more strings.

Can I select multiple ID in jQuery?

The element selector can also be used to select multiple elements. Note: Seperate each element with a comma.

How do I select a specific class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


1 Answers

To find elements within another, use a contextual selector. Try this:

$("p", this).css({ /* ... */ });

Or you can concatenate the id of the parent - although this is a little ugly IMO:

$("#" + this.id + " p").css({ /* ... */ });

Or you can use find() on the parent element:

$(this).find("p").css({ /* ... */ });

Any of the above will work for you, although the second example is pretty ugly and should be avoided. Always use the this reference directly where possible.

like image 145
Rory McCrossan Avatar answered Oct 01 '22 11:10

Rory McCrossan