tags:

views:

25

answers:

2

I need to select all divs 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?

+2  A: 

Use the :has selector:

$("div:has(> object)").css("text-align", "center");

Here's an example I wrote up:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; 
<html>
<head>
<style type="text/css">
div { padding: 15px; border: 1px solid black; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(function() {
  $("div:has(> h3)").css("background", "yellow");
});
</script>
</head>
<body>
<div>
  <h2>outer</h2>
  <div>
    <h3>inner</h3>
  </div>
</div>
</body>
</html>
cletus
@cletus: you're fast! :D Just about to do the same!
fredrik
+2  A: 

You can simplify your way:

$('div:has(> object)').css('text-align', 'center');

..fredrik

fredrik