I am new to javascript and i was wondering can I access the video element thats inside the div element?
<div id="player" class="player">
<video id="element"></video>
</div>
I am new to javascript and i was wondering can I access the video element thats inside the div element?
<div id="player" class="player">
<video id="element"></video>
</div>
Share
Improve this question
asked Aug 20, 2014 at 23:43
user3853858user3853858
2451 gold badge4 silver badges11 bronze badges
1
- How do you access the div element? Can't you access the video element by id ? – Volune Commented Aug 20, 2014 at 23:46
4 Answers
Reset to default 1var el = document.getElementById("element");
If you were using jQuery you could target it like this:
var el = $('video#element');
In pure javascript?
for that you'd use something like var video = document.getElementById('element');
here's a fiddle
I added a console.log statement so you can see the element showing up in the developer console.
Since the element has an id
, you can simply get it directly :
var myVideo = document.getElementById("element");
If it didn't have an id for some reasons, you could do that :
var myVideo = document.querySelector("#player video");
That's very basic JS so you can find help on that anywhere. And specifically on querySelector here, for example.
You can just use regular javaScript to access the video element, but using it's id
and the getElementById
function.
var video = document.getElementById('element');
// Play Video
video.play();
If you're using jQuery, you can also do this:
var $video = $("#element");
$video.get(0).play();