Q:

Check the autoplay attribute exists in iframe src

I want to check whether the attribute 'autoplay' exists in iframe url or not.  If the attribute does not exist, I want to add 'autoplay'.  If it exists I want to change the value of the 'autoplay=1' to 'autoplay=0' and vice versa. 
I have the iframe with dynamic src.  Now whenever I click the play icon autoplay attribute append as https://www.youtube.com/embed/fffjZpMIHk?autoplay=1?autoplay=1.  So I want to check whether the attribute exists and then need to append. 
<div class="video-container" id="video-container">
  <iframe src="https://www.youtube.com/embed/fffjZpMIHk" frameborder="0" allowfullscreen=""></iframe> 

The script I'm now using:
$(".playButton").on('click',function() {
  $(".flex-active-slide iframe")[0].src += "? autoplay=1";
  $('.flex-active-slide').contents().find("iframe").attr('src', function(i, oldSrc) {
    return oldSrc.replace('autoplay=0', 'autoplay=1');
  });
});

Sample fiddle: Demo

A:

Could you please try with below code:
$(function() {
    $(".playButton").on('click',function() {
      var iframeObj = $(".flex-active-slide iframe")[0];
      var currentURL = iframeObj.src;

        if(currentURL.indexOf("autoplay=") == -1)
        {
            //autoplay not present in src; so lets add it
            currentURL = currentURL + '? autoplay=1'
        }
        else{
            if(currentURL.indexOf("autoplay=1")>-1){
                //replace autoplay=1 with autoplay=0
                currentURL = currentURL.replace('autoplay=1', 'autoplay=0');
            }
            else
            {
                //replace autoplay=0 with autoplay=1
                currentURL = currentURL.replace('autoplay=0', 'autoplay=1');
            }
        }

        iframeObj.src = currentURL; //set the newer src to iframe

    });
});

