0

I'm writing a script to detect whether or not an image is present on a webpage. It's a standard formatting with their html for this section. If there is not an image it looks like

    <div id="photo" style="display: none;">
       <img id="image" src="IMAGESOURCE" onerror="img2txt()" alt="">
    </div>    

if there is an image present that same html looks like this

    <div id="photo">
       <img id="image" src="IMAGESOURCE" onerror="img2txt()" alt="">
    </div>

Right now in the script I'm using this, which doesn't work (or i wouldn't be here :D )

  var images = ($('#photo[style*="display: none"]').length === 0 ? false : true);

    if (images) {
      $('#yes[value="Yes"]').click();
    }
    else {
      $('#no[value="No"]').click();
    }

(The clicks are for the radio buttons on the form that I am filling out based on this image query)

As of right now the if/else statement is giving the radio "No" a click on a page where it should be a yes. I've tried using

    if (!images) {
      $('#yes[value="Yes"]').click();
    }
    else {
      $('#no[value="No"]').click();
    }

just to see if my boolean was incorrect. But with that adjustment it just does the opposite again. Any help is much appreciated. Thanks

3
  • You should drop the attribute selectors.. Commented Feb 19, 2016 at 0:28
  • $('#yes').prop('checked', $('#image').is(':visible')) Commented Feb 19, 2016 at 0:30
  • $('#no').prop('checked', !$('#image').is(':visible')) Commented Feb 19, 2016 at 0:30

2 Answers 2

1

So it is always display:none when not present? jQuery has a specific selector for that :visible.

var present = $("#photo").is(":visible");
Sign up to request clarification or add additional context in comments.

1 Comment

It is always display:none when not present... that line of code isn't working for this though...
1

To find out whether the image is visible or not:

$('#image:visible').length;

With reference to your own posted code, you could use:

var imageVisible = $('#image:visible').length,
    toClick = imageVisible ? 'yes' : 'no';

$('#' + toClick + '[value=' + toClick + ']').click();

Or, avoiding the unnecessary attribute-selectors (given that an id is a unique identifier:

$('#' + toClick).click();

References:

4 Comments

I'm not sure why but this isn't working. Also, the id's are actually the same for the yes/no radio buttons (i wrote the html from memory, oops), so the attribute selectors need to stay. But I would think the first suggestions would work... no luck though
In what way is it not working? Where does it fail? And if the id of two separate elements are the same then your JavaScript will break, that's invalid HTML.
It's acting the same as before, either Yes for every page or No for every page depending on how I write the if statement if (!images) vs if (images) . Also, I went and double checked again, but the id's for the radio button inputs are the same... I did not write the html for that.
Then you'll need to post your HTML for us to help.

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.