0

I want to replace the part of the given URL using button click.When I click play button need change autoplay=0 to autoplay=1 and When I click close button need change autoplay=1 to autoplay=0

jQuery(document).ready(function($) {
  $('.playButton').click(function() {
    alert('play');
    $("iframe").src = src.replace(/\&autoplay\=\d/, "&autoplay=" + 1);
  });

  $(".close_icon").click(function() {
    alert('close');
    $("iframe").src = src.replace(/\&autoplay\=\d/, "&autoplay=" + 0);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe src="https://www.youtube.com/embed/C0DPdy98e4c?width=640&autoplay=1"></iframe>
<div class="playButton">Play</div>
<div class="close_icon">Close</div>

6
  • 1
    Use attr() to change the src attribute of iframe. $('iframe').attr('src', function(i, oldSrc) { return oldSrc.replace('autoplay=0', 'autoplay=1'); }); Commented Dec 29, 2016 at 8:35
  • @tushar your post is working fine.Please post it again Commented Dec 29, 2016 at 8:48
  • 1
    Here is complete code Commented Dec 29, 2016 at 8:50
  • post your ans.I'll upvote your answer Commented Dec 29, 2016 at 8:52
  • I need to check autoplay is in src and then append So, there can be cases when there is no autoplay=n in the URL? Commented Dec 29, 2016 at 8:52

2 Answers 2

2

You don't need RegEx for this. String replacement can be done.

$("iframe").src will not set the value of src attribute of iframe. Use attr() to change the attribute of an element.

jQuery(document).ready(function($) {
  $('.playButton').click(function() {
    $("iframe").attr('src', function(i, oldSrc) {
      if (oldSrc.indexOf('autoplay') === -1) {
        // If autoplay is not on URL, add it
        oldSrc += '&autoplay=1';
      }

      return oldSrc.replace('autoplay=0', 'autoplay=1');
    });
  });

  $(".close_icon").click(function() {
    $("iframe").attr('src', function(i, oldSrc) {
      return oldSrc.replace('autoplay=1', 'autoplay=0');
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe src="https://www.youtube.com/embed/C0DPdy98e4c?width=640"></iframe>
<div class="playButton">Play</div>
<div class="close_icon">Close</div>


The code can be DRY'd as follow

jQuery(document).ready(function($) {
    // To update the value of autoplay in the URL of iframe src attribute
    var changeAutoplay = function(newValue) {
        $('iframe').attr('src', function(i, oldSrc) {
            return oldSrc.replace(/autoplay=\d/, 'autoplay=' + newValue);
        });
    };

    $('.playButton').click(function() {
        changeAutoplay(1);
    });

    $(".close_icon").click(function() {
        changeAutoplay(0);
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I need to check autoplay is in src and then append
@user3386779 To check if a string exists in other string, you can use indexOf(). In your case you can also use /autoplay=\d/.test(url)
@user3386779 I've added condition in if in the play callback. I guess there is no need to do same in stop callback.
@user3386779 I've removed the autoplay from the URL in the above snippet. You can see autoplay is added when you click on play button.
1

As you can see, there is an error from the console Uncaught ReferenceError: src is not defined, which means that you try to use non-existent property, try with:

$("iframe").attr("src", function(index, value){ return value.replace("autoplay=0", "autoplay=1") })

Read more about attr method here.

Comments

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.