6

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

2
  • 1
    seems to work fine jsfiddle.net/44W2s Commented Sep 21, 2012 at 6:50
  • did u put that inside the $(document).ready()? Commented Sep 21, 2012 at 6:51

4 Answers 4

6

You should do your verification inside document ready function

$(document).ready(function(){
   if($('div.previewWrapper div.previewContainer img').attr('src') == '') { 
      alert('got me'); 
    }
});
Sign up to request clarification or add additional context in comments.

Comments

4

try this code:

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

1 Comment

He asked how to "get" the click event, too. $("div.previewWrapper div.previewContainer img[src=='']").click(function() { alert('got me'); });
2

You should wrap this in document.ready function like this

$(document).ready(function(){

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


});

1 Comment

that will check the first image attribute src only.
2

Please Check this It seems working 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 Comment

It's better to post the sample code here, too. JSFiddle may disappear one day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.