0

To make it easier to understand please take a look at the Codepen: http://codepen.io/hennysmafter/pen/YqmLKR

The codepen is a simpler version of the actual code with only the parts that are necessary!

In the codepen you have two popups one popup has a video element with autoplay and the other has a video element without autoplay

On document ready all video elements on the page are paused this cannot be changed because of other videos in the actual code

Now I just need to have the code adapted so that:

  1. On click popup opens and class is added opened (already there)
  2. If video element inside popup has autoplay then play the video (not working)
  3. Or no autoplay so do nothing

    if ($('.modal-box.opened').find('video').attr(autoplay) == true) {
        $('.modal-box.opened').find('video').each(function() {
          $(this).get(0).play();
    });
    }
    
0

1 Answer 1

2

You've two somewhat common mistakes: You only used 1 equals sign in the part where you check if autoplay is true.

A single = is used for setting variables where as two is used for checking equality.

Edit: You are also attempting to use traditional HTML style attribute checking on a jQuery object. To check an attribute in jQuery, you need to use .attr(attributeName)

Therefore, the correct line would be if ($('.modal-box.opened').find('video').attr(autoplay) == true) {

5
  • Thank you for your answer. I changed the code but now the videos will no longer work at all. I don't necessarily want to keep this code I just need to have a code that checks if the word autoplay is present inside the <video> tag. And I think it ain't working as it is looking for <video autoplay="true"> instead of <video autoplay>. Deserves a upvote however. But not marking as accepted just yet. Commented May 23, 2016 at 19:22
  • I've edited the post with more info.
    – figgyc
    Commented May 23, 2016 at 19:27
  • I have no clue why but it ain't working I am going to put it in Codepen! Not saying your answer is wrong because I think it is not it's something else I think! Commented May 23, 2016 at 19:27
  • I added a codepen for you to see codepen.io/hennysmafter/pen/YqmLKR Commented May 23, 2016 at 19:45
  • So I just rewritten the question to make thing easier to understand. Thanks for helping. Commented May 23, 2016 at 20:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.