0

I want my button in code below to have click event but not parent div.

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>

<div id="myVideo">
    <video controls="" autoplay="" name="media">
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
    </video>
    <button id="myButton">Play Video</button>
</div>


</body>
</html>

I tried using css pointer events but if parent has it then child button is not working.

4 Answers4

1

add onclick event handler like this: <button id="myButton" onclick="function(e) {e.stopPropagation();}">Play Video</button>

event.stopPropagation stops the click event propagation to parent elements. Learn more here: https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

netchkin
  • 1,387
  • 1
  • 12
  • 21
0

I believe this is what you're looking for:

<div id="myVideo">
  <video name="media" id="video">
    <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
  </video>
</div>

<button id="myButton" onclick="document.getElementById('video').play()">Play Video</button>
Okx
  • 353
  • 3
  • 23
0

var myVideo = document.getElementById("myVideo"); 

function playPause()
{ 
    if (myVideo.paused) 
        myVideo.play(); 
    else 
        myVideo.pause(); 
} 
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>

<div id="mydIV">
    <video ID="myVideo" controls="" autoplay="" name="media">
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
    </video>
    <button onclick="playPause()">Play/Pause</button> 
</div>


</body>
</html>

This will help you
Happy Coding....

Yash Parekh
  • 1,513
  • 2
  • 20
  • 31
0

You've at least two options. The first is to check whether the event was fired by the button or the div. Or to prevents the event from bubbling up the DOM tree. In jQuery you can do that by calling the method "stopPropagation()".

Here the answer to a similar question.

Elmehdi93
  • 70
  • 1
  • 9