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?
The concat() method joins two or more strings.
The element selector can also be used to select multiple elements. Note: Seperate each element with a comma.
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.
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.
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