Before I ask the question refer to this jsFiddle: http://jsfiddle.net/AxGHA/3/
I basically want to remove the float: left; StyleSheet attributes on the H2 and P tags IF no image exists within the div with jQuery. Any ideas?
Thanks!
Before I ask the question refer to this jsFiddle: http://jsfiddle.net/AxGHA/3/
I basically want to remove the float: left; StyleSheet attributes on the H2 and P tags IF no image exists within the div with jQuery. Any ideas?
Thanks!
you can use :not and :has I have done an example code below:
$(".section:not(:has(>img))").addClass("no-image");
and the CSS for that:
.no-image h2, .no-image p {
float: none;
width: auto;
}
example: http://jsfiddle.net/voigtan/AxGHA/5/
">img" instead:
http://jsfiddle.net/voigtan/AxGHA/6/
I'm adding a css class to the .section and style it in the css on how I want it to look.
:has(img) to :has(>img) to see if the .section has a direct child of an image: jsfiddle.net/voigtan/AxGHA/6You can do this
$(".section").each(function() {
var hasImage = $(this).children("img").length != 0;
if (!hasImage) {
$(this).find("h2,p").css("float", "none");
}
});
Check out the updated jsFiddle
I think you can do something like this (notice that I didn't test the code):
$('h2, p').parent('div').each(function(index, $element) {
if (!$element.has('img'))
$element.css('float', 'none');
});
Notice that I can't view the jsfiddle from where am I so I based the answer directly on what you explained.