playButton = document.getElementsByName('playButton')[0]; you will get an empty array here
you need to add name attribute to your input tag or change to
playButton = document.getElementById('playButton');
سؤال
I am trying to get an input image to change through javascript when clicked but I get the following error through Google Chrome debug:
Uncaught TypeError: Cannot call method 'addEventListener' of undefined
The Following is my HTML code:
<section id="skin">
<video id="mainPlayer" width="640" height="360">
<source src="video.mp4">
</video>
<nav>
<div id="buttons">
<input type="image" src="play_button.png" id="playButton" width="22" height="22">
</div>
<div id="defaultBar">
<div id="progressBar"></div>
</div>
<div style="clear:both"></div>
</nav>
And this is my javascript code:
function doFirst() {
mainPlayer = document.getElementById('mainPlayer');
playButton = document.getElementsByName('playButton')[0];
playButton.addEventListener('click', playOrPause, false);
}
function playOrPause() {
if (!mainPlayer.paused && !mainPlayer.ended) {
mainPlayer.pause();
playButton.src="controls.png";
window.clearInterval(updateBar);
} else {
mainPlayer.play();
playButton.src="play_button.png"
updateBar = setInterval(update, 500);
}
}
window.addEventListener('load',doFirst,false);
المحلول
playButton = document.getElementsByName('playButton')[0]; you will get an empty array here
you need to add name attribute to your input tag or change to
playButton = document.getElementById('playButton');