0
if(.is-open close-now){
jQuery(".extra_div").click(function($){
            alert('"Sorry, this takeaway only accepts orders during its opening hours"');
        });
}

I think something like the above is what I need but I don't know how the if statement part should work out.

I'm looking to check if a certain div is present and then if it is, run the following jQuery on click code.

Update 1

if ($(.is-open close-now).length){
jQuery(".extra_div").click(function($){
            alert('"Sorry, this takeaway only accepts orders during its opening hours"');
        });
}

The two divs are

is-open open-now - open .is-open close-now - closed

  • IS the div always present and possibly hidden? Or if the hours are outside business hours, the div doesn't exist on the page? – Ryan Wilson Jun 04 '18 at 13:19
  • Presence check in DOM: if($(_selector_div_).length){ – mscdeveloper Jun 04 '18 at 13:22
  • Sorry, picked this job up after but from what I can tell if it's closed and has the above div, then the other one isn't present at all –  Jun 04 '18 at 13:23

1 Answers1

0

You can easily check its length. If a certain element doesn't exist then checking its length returns false

if($('.is-open').length){
  // exists
  console.log('is-open exists');
  // do stuff
}
if($('.not-is-open').length){
  // doesn't exist
  console.log('not-is-open exists');
  // do stuff
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="is-open">
</div>
Hearner
  • 2,711
  • 3
  • 17
  • 34
  • Roughly what i'm going for but I updated post with the 2 divs, would it essentially be this but I swap in my ones –  Jun 04 '18 at 13:33