Code
window.addEventListener('load', iniciar, false);
function iniciar() {
var imagen = document.getElementById('imagen');
imagen.addEventListener('mouseover', peligro, false);
}
function peligro() {
var imagen = document.getElementById('imagen').src = "https://img.utdstc.com/icons/256/app-danger-check-android.png";
}
<body>
<img src="http://requirementsinc.com/img/icon/PNG/danger.png" id="imagen">
</body>
Explanation
The problem is that you are doing this:
var imagen = document.getElementById('imagen').innerHTML = "https://img.utdstc.com/icons/256/app-danger-check-android.png";
When you should do this instead:
var imagen = document.getElementById('imagen').src = "https://img.utdstc.com/icons/256/app-danger-check-android.png";
What happens when you change the attribute .innerHTML
is that you change what is inside your image element, that is, what is found after >
to <
, while changing the attribute src
, you really modify the origin of the image of the tag.
Update
I have left an example that could help you if you want the mouse to restore the original image.
window.addEventListener('load', iniciar, false);
function iniciar() {
var imagen = document.getElementById('imagen');
imagen.addEventListener('mouseover', peligro, false);
imagen.addEventListener('mouseout', restaurar, false);
}
function restaurar(){
var imagen = document.getElementById('imagen').src = "http://requirementsinc.com/img/icon/PNG/danger.png";
}
function peligro() {
var imagen = document.getElementById('imagen').src = "https://img.utdstc.com/icons/256/app-danger-check-android.png";
}
<body>
<img src="http://requirementsinc.com/img/icon/PNG/danger.png" id="imagen">
</body>