How can I remove every link that points to a video after the video ends?

Asked

Viewed 26 times

1

I have this div below, namely with id and having within it the elements p followed a(anchor):

var vid = document.getElementById('player');

var link = document.getElementById('lista').getElementsByTagName('a');

var el = document.getElementById('lista').getElementsByTagName('p');

var src0 = vid.src;

var href0 = link.href;

var fonte = [
	"https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.webm",
	"https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.webm",
	"https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.webm"
],
    clone = [
	"https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.webm",
	"https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.webm",
	"https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.webm"
];

    function proximo() {
    	if (fonte.length === 0) {
        	fonte = clone.slice(0);
    	}
    	var indice = fonte.splice(0, 1);
    	vid.src = indice;
    	vid.autoplay = indice;
	}
window.setInterval(function() {
    	var seg = vid.duration - vid.currentTime;

    	if ((seg - 1) < 0)

	for (var i = 0; i < el.length; i++) {

	if(src0 == href0) // comparar se o atributo src no elemento video e igual a do atributo href

	el[i].outerHTML = '';

	delete el[i].item(0);

		proximo();

	break;
		}
	}, 1000);

proximo();
<video id="player" preload controls></video>

<hr>

<div id='lista'>
	    <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.webm">Animais cantando</a></p>
	    <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.webm">Equipment these days</a></p>
	    <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.webm">Peck Pocketed</a></p>
</div>

Well, take a good look at my source code and note that I am doing this automatically, without clicking directly on the link.

1 answer

1


Usa el[i].parentNode.removeChild(el[i]); instead of delete el[i].item(0);

var vid = document.getElementById('player');
var link = document.getElementById('lista').getElementsByTagName('a');
var el = document.getElementById('lista').getElementsByTagName('p');

var src0 = vid.src;
var href0 = link.href;
var fonte = [
  "https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.webm",
  "https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.webm",
  "https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.webm"
];
var clone = fonte.slice();

function proximo() {
  if (fonte.length === 0) {
    fonte = clone.slice(0);
  }
  var indice = fonte.splice(0, 1);
  vid.src = indice;
  vid.autoplay = indice;
}
window.setInterval(function() {
  var seg = vid.duration - vid.currentTime;
  if ((seg - 1) < 0)
    for (var i = 0; i < el.length; i++) {
      if (src0 == href0) // comparar se o atributo src no elemento video e igual a do atributo href
        el[i].outerHTML = '';
      el[i].parentNode.removeChild(el[i]);
      proximo();
      break;
    }
}, 1000);

proximo();
<video id="player" preload controls></video>
<hr>
<div id='lista'>
  <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/animais_cantando.webm">Animais cantando</a></p>
  <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/equipment_these_days.webm">Equipment these days</a></p>
  <p><a href="https://sites.google.com/site/mplayerplugin/repositorio/peck_pocketed.webm">Peck Pocketed</a></p>
</div>

  • It seems that it worked with its applied method! It was just a detail that had ignored to use el[i].parentNode.removeChild(el[i]); instead of delete el[i].item(0);, for the delete el[i].item(0); would become Cross-browser. But unfortunately for this did not work :(

  • @Diegohenrique delete el[i].item(0); removes the element from the list that . getElementsByTagName gives, but does not remove from the page. To remove from the page you have to use the method . removeChild()

Browser other questions tagged

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