agon024 agon024 - 1 month ago 8
jQuery Question

jQuery - remove video auto play on click

I am using:

$("#vid1")[0].src += "&autoplay=1";


to automatically start a video when a button is clicked and its sets the source as:

https://www.youtube.com/embed/LKFrlPDM6CE?rel=0&autoplay=1


How can I remove the autoplay=1 and set it to autoplay=0 when I click a second button.

Thanks

Answer

Give this a try

var newSource = $("#vid1").attr('src').replace("&autoplay=1", "&autoplay=0");
$("#vid1").attr('src', newSource );

A few things to note:

$("#vid1")[0]

The [0] isn't needed. You're selecting an ID and it should only appear once. If you were selecting a class that could appear more than once you'd use .eq(0)

This will only work if you've already appended the &autoplay=1 to the src. If this is a problem add a check to see if it's there or not.

Comments