Jump to content

Display none if src is empty?


stalwil

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...