addEventLstener does not work well

0

What I want is for the video to be played in an android browser and the only way that works for me until now is with the event that I already have that is with "click" but what I want is that it be played when start the page and try load and onload

This is my video

 <video id="video" class="slider-video" width="100%" height="auto" 'preload="" autoplay="autoplay" style="visibility: visible; width: 100%; " poster="imgs/video.jpg" >'
                <source src="imgs/video.webm">
                <source src="imgs/pruebai.webm type='video/webm; codecs="vp8, vorbis"'>

 </video>

This is my script

var video = document.getElementById('video');
    window.addEventListener("click",function(){
  video.play();
    },false);
  });
    
asked by Godeolo 06.06.2017 в 22:30
source

1 answer

0

You can try EventTarget.dispatchEvent () , this triggers the event associated with the html element, in the following example, trigger in the onClick event associated with the <button id="alert"> element.

Example:

document.getElementById("alert").addEventListener("click",getAlert);

function getAlert(){
 alert("Hola");
}
// Aquí disparo el evento onClick asociado al <button id="alert">
document.getElementById("alert").dispatchEvent(new MouseEvent('click'));
<button id="alert">Alert("Hola");</button>

For versions before IE9 you must use the EventTarget method .fireEvent () .

If you want a really easy solution compatible with all browsers, I recommend you use jQuery .

Example:

document.getElementById("alert").addEventListener("click",getAlert);

function getAlert(){
 alert("Hola");
}
// Aquí disparo el evento onClick asociado al <button id="alert">
$("#alert").click();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="alert">Alert("Hola");</button>
    
answered by 07.06.2017 в 01:23