Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if img src is empty using jQuery

I have the following HTML:

 <div class="previewWrapper" id="thumbPreview3">
  <div class="previewContainer">
   <img src="" class="photoPreview" data-width="" data-height=""><span>3</span>
  </div>
 </div>

And I have the following JQUERY which isn't working.

    if($('div.previewWrapper div.previewContainer img').attr('src') == '') {
      alert('got me');
    }

can anyone advise what I'm missing. What to get the click event to work when the src is empty.

thx

like image 803
Adam Avatar asked Sep 21 '12 06:09

Adam


1 Answers

You should do your verification inside document ready function

$(document).ready(function(){
   if($('div.previewWrapper div.previewContainer img').attr('src') == '') { 
      alert('got me'); 
    }
});
like image 145
Mihai Matei Avatar answered Sep 19 '22 11:09

Mihai Matei