stalwil Posted August 18, 2010 Share Posted August 18, 2010 Hey Guys,I hope this is easy. I'm trying to figure out how to add "display none" to an image if the src is empty! This is what i have so far: <script>var $j = jQuery.noConflict();$j(document).ready(function(){ var testing = $j("img#logoTest").attr("src"); if(testing == ""){ $j("img#logoTest").css("display","none"); }});</script> Can you tell me what im doing wrong?Thanks! Link to comment Share on other sites More sharing options...
chibineku Posted August 18, 2010 Share Posted August 18, 2010 Have you tried alerting the value of testing, to see what it contains? It seems like it should work from looking at it. Is there a src attribute for the image in question, or have you missed it out completely? If there is no src, then testing will be FALSE or NULL and not ''. Link to comment Share on other sites More sharing options...
stalwil Posted August 18, 2010 Author Share Posted August 18, 2010 Have you tried alerting the value of testing, to see what it contains? It seems like it should work from looking at it. Is there a src attribute for the image in question, or have you missed it out completely? If there is no src, then testing will be FALSE or NULL and not ''.Yes, i placed an alert on there and now it works fine. I guess the problem starts when there are multiple img tags with the id logoTest!so if its just one image then its fine:<img sr=""/>if its two then it reads the first one and than attaches display none to both and vice versa:<img src=""/><img src="myImage.jpg"/>I need it to distinguish between the two. Is that possible? Link to comment Share on other sites More sharing options...
ShadowMage Posted August 18, 2010 Share Posted August 18, 2010 You can't have more than one element with the same id. Not only is it invalid, but it causes issues with JS as you found out. You might want to consider targeting the image tags themselves and use jQuery's .each() function. $j('img').each(function() { var testing = $j(this).attr("src"); if(!testing){ $j(this).css("display","none"); }}); Something like that I think. I've never really used jQuery before, but I've seen alot of it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.