Error picking element by class (but with ID)

Asked

Viewed 200 times

0

I’m trying to get a video element from CLASS,(something that seems silly to me), But I’m not getting it, take my example:

<script src="jquery-3.2.1.js"></script>

    <video   id="Video1" class="video-stream" src="https://www.html5rocks.com/pt/tutorials/video/basics/Chrome_ImF.mp4">
    </video>

    <div id="buttonbar">
        <button id="play"       onclick="vidplay()" >&gt;</button>
    </div>      


    <script>
      function vidplay() {
      // com JS e pegando pelo ID funciona bem:
      var video = document.getElementById("Video1");

      // com JS e pegando pela CLASS  nao vai...
        //var video = document.getElementsByClassName("video-stream");

      // com JQUERY NADA VAI! ..
      //var video = $("#Video1");
      //var video = $(".video-stream");

        // um teste....
            $.each( $( ".video-stream" ), function() {
                console.log( "  MESSAGE ====>  | id = " + this.id );
            });

      // o erro aparece quando dou play no video...
      video.play(); 
    }
    </script>

I need to get that element of < video > by his CLASS... someone can tell me what’s wrong here?

this is my code

3 answers

5


The problem when trying to play per class in this case is that there may be more than one element with the same class and both jquery and getElementsByClassName will not return only one element, but a collection of them.

If you want to play only one video, I advise you to select it by ID since it is unique. If there are several and you want to play them all with one button, you will have to make a loop through the elements, for example: $('.video-stream').each(function(){this.play()})

If you still want to use class for a video only you can use: document.getElementsByClassName("video-stream")[0]; or $(".video-stream")[0];

There are also other ways to use . get() (not to be confused with $.get()) from jquery.

  • +1 for incrementing jQuery in the answer. In this case, obtaining an element by class is more relaxed by jQuery than by pure Javascript

  • YES I need to pick by class. And thank you so much for the help, now I can do a js and jquery!!! Well I’m still studying java... I have a lot to learn!!! And of course I didn’t realize I had to take the first element...!!!!

1

EDIT: var video picking up only Class element. Get() method giving play on 2nd element with Class video stream.

Your jQuery is correct.

The error occurs because play() is not a function of jQuery, but of the DOM element.

To get around it, instead of video play.() you use .get():

video.get(0).play()

Solution Reference

Following example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<video   id="Video1" class="video-stream" src="https://www.html5rocks.com/pt/tutorials/video/basics/Chrome_ImF.mp4">
</video>

<video   id="Video2" class="video-stream" src="https://www.html5rocks.com/pt/tutorials/video/basics/Chrome_ImF.mp4">
</video>

<div id="buttonbar">
    <button id="play"		onclick="vidplay()"	>&gt;</button>
</div>

<script>
  function vidplay() {  
  
  //No jQuery eu peguei a ID(#Video1) e a Class(.video-stream) juntos
  var video = $(".video-stream");
  
	// um teste....
	   	$.each( $( ".video-stream" ), function() {
			console.log( "  MESSAGE ====>  | id = " + this.id );
		});
	 
  // o erro aparece quando dou play no video...
  //video.play();
  
  //Função play() funcionando
  video.get(1).play();
  
}
</script>

  • BUT in your example.... Oce is using ID... I CAN’T USE ID ?!?!?!? this is very important in solving my problem.... I even put very big logo in the title of the post.....

  • Oops! My lack of attention. In this case you can only pass the Class in var video = $(".video-stream");. In video.get(0).play();, the 0 means he’s gonna play first element that has the Class video stream. So, if you need to play a 2nd element with the same Class, you should put 1 and so on.

  • Oh yes! I think I’m beginning to understand! Thank you!

0

Look at this example:

    function vidplay() {
       var video = document.getElementById("Video1");
       var button = document.getElementById("play");
       if (video.paused) {
          video.play();
          button.textContent = "||";
       } else {
          video.pause();
          button.textContent = ">";
       }
    }

    function restart() {
        var video = document.getElementById("Video1");
        video.currentTime = 0;
    }

    function skip(value) {
        var video = document.getElementById("Video1");
        video.currentTime += value;
    }    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<video id="Video1" >
     <source src="https://www.html5rocks.com/pt/tutorials/video/basics/Chrome_ImF.mp4" type="video/mp4" />
</video>

<div id="buttonbar">
    <button id="restart" onclick="restart();">[]</button> 
    <button id="rew" onclick="skip(-10)">&lt;&lt;</button>
    <button id="play" onclick="vidplay()">&gt;</button>
    <button id="fastFwd" onclick="skip(10)">&gt;&gt;</button>
</div>

  • So... once again.. I appreciate the help BUT in your example.... you are using ID... I CANNOT USE ID !!!!! This is very important in solving my problem.... I even put very large logo in the title of the post.....

Browser other questions tagged

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