Make sure 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 that does not work.

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

Someone can tell me what I am missing. What makes the click event work when src is empty.

THX

+5
source share
4 answers

try this code:

$(document).ready(function(){
    if ($("div.previewWrapper div.previewContainer img[src=='']").click(function()){
          alert('got me');
        }
});
+3
source

You must complete your check inside the document ready function

$(document).ready(function(){
   if($('div.previewWrapper div.previewContainer img').attr('src') == '') { 
      alert('got me'); 
    }
});
+5
source

You should wrap this in a document.ready function like this

$(document).ready(function(){

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


});
+1
source

Please check it. Seems to work here

<div class="previewWrapper" id="thumbPreview3">
  <div class="previewContainer">
   <img src="" class="photoPreview" data-width="" data-height=""><span>3</span>
  </div>
</div>
<input type="button" id=click value =" Click me" />


$(function () {

    $("#click").click(function () {

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


});​
+1
source

All Articles