Change Videos from Canvas

Asked

Viewed 490 times

0

Good Folks I have a screen that incorporates Youtube videos

<div class="row centro-video">
  <iframe width="853" height="480"  class="fundo-video" id="video-curso1" src="https://www.youtube.com/embed/kRUyL6cJ7W4?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>

I’m uploading a list of videos from the database, which has the video title, and the video link, I’m creating a button for each video.

<?php
  foreach ($listaVideo as $videoobj) {
?>
  <a href="<?php echo $videoobj->getLink()?>" data-dismiss="modal" class="btn btn-block btn-primary texto-grande" type="button"> <?php echo $videoobj->getTitulo()?></a>
<?php
  }
?>

The doubt is the following, how do I change the IFRAME video when the button is clicked ? with javascript or jquery

  • What you want would be to change the font (src) video by clicking a button?

  • i wanted to change the link of the iframe, with the link that I get from the bank in the case in the part $videoobj->getLink()

2 answers

1

Make the buttons change the src attribute of the iframe, see (note that I have set an id to the iframe):

Jquery:

$("#idiframe").attr("src", "linkvideo");

Javascript:

document.getElementById('idiframe').src ='linkvideo';

@Edit: To get what you want, replace the href attribute of the link to '#' thus

<a href="#">

After doing this assign the code that I passed you to a function, to avoid repeating code.

function mudarVideo(url){
    document.getElementById('idiframe').src = url;
}

In the buttons being created by the for loop, add the following attribute:

onclick="mudarVideo('<?php echo $videoobj->getLink();?>');?>"

Getting at the end like this:

<?php
    foreach ($listaVideo as $videoobj) {
?>
    <a href="#" onclick="mudarVideo('<?php echo $videoobj->getLink();?>');?>"  data-dismiss="modal" class="btn btn-block btn-primary texto-grande" type="button"> <?php echo $videoobj->getTitulo()?></a>
<?php
    }
?>

Should solve your problem...

  • yes right, but as I’ll get the links from the videos, if I’m creating the buttons from a for, they won’t have id set

  • I don’t quite understand, you’re creating the buttons from a for, and they won’t have id set, but, the buttons don’t need an id, just iframe.

  • Right, but how will I change the src of the iframe, if I have no way to capture the button

  • I edited the post, read and tell me if solved.

  • That was the point

1


In this case, just make a function that changes the attribute src iframe, example:

function changeVideo($a) {
  document.getElementById('video-curso1').src = $a.href;
  return false;
}
<a href="https://www.youtube.com/embed/kRUyL6cJ7W4?rel=0&amp;showinfo=0" onclick="return changeVideo(this)">Video 1</a>
<a href="https://www.youtube.com/embed/cHupkv2cEhs?rel=0&amp;showinfo=0" onclick="return changeVideo(this)">Video 2</a>
<div class="row centro-video">
  <iframe width="853" height="480" class="fundo-video" id="video-curso1" src="https://www.youtube.com/embed/kRUyL6cJ7W4?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>

  • Right, but you realized that the buttons are being created from a for, I want to know how I will recover each link of this, being from the said button that was clicked

  • I changed the example to receive the href attribute link from the <a> tag, as in your code. Then you just need to add the onclick event as per the example

  • That’s right, now it’s worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.