No display if there are no images in jquery
I am using jquery to display src of images: none when src = "". Looks like,
<div class="parent">
<img src="images_path/or nothing" class="Images"/>
<div class="content"></div>
</div>
the contents of the float class stayed in line with the width of the images in the parent div. Now I want to detect the image source when it is empty. I changed the content.width class to match the parent div of its selft. My jquery code like this
var CustomizeImage = $(".CategoryNews-left").find(".Images").attr("src");
alert(CustomizeImage);
if(CustomizeImage == ""){
alert("ko co gi");
$(".CategoryNews-left").find(".Images").css("display","none");
$(".CategoryNews-left").find(".news").css("width","320px");
$(".CategoryNews-left").find(".Content").css("width","320px");
}
But the content of the class doesn't get the new width, not even src = empty, someone helps me fix this trick, thanks for reading my note !!!
a source to share
Thanks to Tobias and Birman, I just checked that my class name in the css file is Content. and I mean I have a list of parent divs. But my code just warns about the first one when I code like this:
var CustomizeImage = $(".CategoryNews-left").find(".Images").attr("src");
alert(CustomizeImage);
if(CustomizeImage == ""){
alert("ko co gi");
$(".CategoryNews-left").find(".Images").css("display","none");
$(".CategoryNews-left").find(".news").css("width","320px");
$(".CategoryNews-left").find(".Content").css("width","320px");
} else {
if(CustomizeImage.indexOf('src=""') > 0){
alert("co nhung rong");
$(".CategoryNews-left").find(".Images").css("display","none");
$(".CategoryNews-left").find(".news").css("width","320px");
$(".CategoryNews-left").find(".Content").css("width","320px");
}
}
and my html code looks like this:
<div class="CategoryNews-left">
<div class="Images">
<img src="images/Anhto_listtin_trai.gif" alt="anh tin" />
</div>
<div class="news">
<h3>blahblahblahblahblah</h3>
<p>blahblah- blahblahblah</p>
</div>
<div class="Content">blahblahblahblahblahblah</div>
<div class="relateNews">
<ul>
<li><a href="">blahblahblahblahblahblahblahblahblahblah</a> <font>VnExpress</font></li>
</ul>
a source to share
Not really sure what you are trying to do, but your first choice is wrong.
$(".CategoryNews-left").find(".Images").attr("src");
Finds the "image" div and searches for it src
. Div doesn't have src. You must try
$(".CategoryNews-left .Images img").attr("src");
Also, keep in mind that attr("src")
only the src of the first img will return, even if you have a lot of them.
a source to share