0

If my image doesnt contain a src then i want to hide it using visibility hidden:

<img border="0" src="" style="cursor:hand;cursor:pointer;" id="EntityPic543">

How can i do this with jquery?

1
  • 2
    Please note that if your img tag has width & height attributes it will still occupy some space when using visibility:hidden. Most of the answers will make your img display:none; Commented Jul 19, 2012 at 11:03

5 Answers 5

4
$('img').filter(function(index){return $(this).attr('src')==='';}).hide();
Sign up to request clarification or add additional context in comments.

Comments

2
$(document).ready(function(){
  $("img").each(function(){
     (!this.src || $(this).prop("src")) && $(this).hide();
  });
});

Thanks to @GeorgeMauer

6 Comments

Why jquery(this) just to get an src attribute?
Also note that if the attribute is missing entirely this will not hide it since attr('src') is undefined (going through the DOM will). If you really want to keep this form then $(this).attr('src')||'' will do it.
Try my code it will work 100% even if there is src attribute is missing.
$('<img>').attr('src')=="" will return false so the image will not hide jsfiddle.net/LafSw
OK. updated my answer. it is hybrid of your answer and mine. Your solution !this.src && $(this).hide() will not work if the image already has attribute src="" jsfiddle.net/LafSw/2
|
1
$('img').each(function() { 
  !this.src && $(this).hide()
});

2 Comments

This will not work if placed in head tag. you have to add it to document ready
That is true, yes, I assumed he was already but good point. It's not a great idea to include all scripts from the head btw
0

Try this without any loop:

$('img[src=""]').hide();

DEMO

4 Comments

Not exactly in the OP but this won't work in the case where the src attribute is missing entirely ([src] is redundant in that case).
if src contains "images/Category/large/469.jpg" this will still hide the image which is wrong. please correct and i will accept your answer
@Super1 its working for me jsfiddle.net/xx62q/6.. if you have problem... please make a fiddle
0
$("#EntityPic543").css("visibility", "hidden"); 

That will hide the element.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.