I need to select all div
s which contain object
as their direct child. :has
just checks for descendants of any kind, so now I'm using:
$('div > object').parent().css('text-align', 'center');
is there a more direct way?
You can simplify your code to:
$('div:has(> object)').css('text-align', 'center');
Use the :has
selector:
$("div:has(> object)").css("text-align", "center");
Here's an example I wrote up:
$(function() {
$("div:has(> h3)").css("background", "yellow");
});
div { padding: 15px; border: 1px solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div>
<h2>outer</h2>
<div>
<h3>inner</h3>
</div>
</div>
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