Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to addeventlistener for a variable referencing a static element without an ID attribute for video tag?

the event doesn't fire for resizing the video tag, i reference the video tag with the variable A which I call dynamically in playvid() function. I have named an id in the video tag and then called videotagId.addeventlistener, but I wanted to try to reference this video tag without specifically callings its ID. I was trying to beautify maximizing screen space for mobile devices on a video editing app but I realize that I have to wait until after I setAttribute(height or width) on the video tag for the resizing event (its not asap), once this fires I can successfully query video.videoWidth or video.offsetWidth and do new math on centering the video tag. if I do video.setAttr('width',number); and then video.videoWidth it will not be reflected new change until the resize event fires

I've checked google and all I can find is people attaching CLICK events to the body element and then parsing through every single click event until you find the one you're looking for, which isn't exactly what I"m doing.

HTML:
    <style>
 
      body {
          color:white;
          background:black;
      }
  </style>
 
  <script>



function playvid(e) {
 
     var a = e.previousElementSibling;
    console.log(a.src);
 


         a.addEventListener('resize', (e) => {
   alert('this does not fire?');
             //in another instance it complains a is  undefined

   });
 
 
 
    setTimeout( function () {
        a.setAttribute('height', window.innerHeight- e.offsetHeight -4);
        a.play();
    }, 500);

}
 
 
 

 
 
  </script>



 
 
  <video style="border: 3px solid red;" src="https://file-examples.com/storage/fe59cbbb63645c19f9c3014/2017/04/file_example_MP4_480_1_5MG.mp4"></video>



 
    <div onclick="playvid(this)" style="cursor:pointer; font-size:40px; border:1px solid green;" >
      click here</div>
 
Last edited:
This does NOT fire the event:
HTML:
  <video id="bob" style="border: 3px solid red;" src="https://file-examples.com/storage/fe59cbbb63645c19f9c3014/2017/04/file_example_MP4_480_1_5MG.mp4"></video>
 
    <div onclick="playvid(this)" style="cursor:pointer; font-size:40px; border:1px solid green;" >
      click here</div>
 
 
 
  <script>
      function playvid(e) {
 
     var a = e.previousElementSibling;
    console.log(a.src);
    

      
               bob.addEventListener('resize', (e) => {     
   alert('this does not fire?');
             //in another instance it complains a is  undefined

   });


        a.setAttribute('height', window.innerHeight- e.offsetHeight -4);
        a.play();


}
      

      
      
</script>


however if i Put:
JavaScript:
            bob.addEventListener('resize', (e) => {     

   alert('this does not fire?');

             //in another instance it complains a is  undefined



   });


outside the function, it will fire
 

New Threads

Buy us a coffee!

Back
Top Bottom