onclick event does not work in chrome!

1

I am not able to perform a transition exercise. I have the img of the lamp erased and another one lit and a ragged one! Until the access I did, but when it's pa, breaking the code does not work!

Here is the source code:

<!DOCTYPE  html>
<html>
<head>
    <title>teste javascript </title>
    <meta charset="UTF-8"/>
    <script>
    function acendeLampada(){
    document.getElementById("Luz").src = "_imagens/Lampada-acesa.jpg ";
    }
    function apagaLampada() {
    document.getElementById("Luz").src = " _imagens/Lampada-apagada.jpg ";
    }
    function quebraLampada() {
    }
    document.getElementById("Luz").src = "_imagens/lampada-quebrada.jpg ";
    </script>
</head>
<body>
<h1> Acenda a lâmpada</h1>
<img src="_imagens/Lampada-apagada.jpg" id="Luz" onmousemove="acendeLampada()" onmouseout=" apagaLampada()" onclick="quebra-Lampada()"/>
</body>
</html> 
    
asked by anonymous 28.10.2017 / 22:03

2 answers

3

There are basically 2 errors in your code:

1. Out of function action:

function quebraLampada() {
    }
    document.getElementById("Luz").src = "_imagens/lampada-quebrada.jpg ";

Correct:

function quebraLampada() {
    document.getElementById("Luz").src = "_imagens/lampada-quebrada.jpg ";
}

2. Function not present in tag <img> :

onclick="quebra-Lampada()"

Correct:

onclick="quebraLampada()"

OTHER PROBLEMS:

Although it may work, it is not good practice to leave these blank spaces at the beginning or end of sequences like:

" _imagens/Lampada-apagada.jpg "; RUIM
"_imagens/Lampada-apagada.jpg"; BOM
    
28.10.2017 / 22:26
2

As far as I can tell, its broken function is by opening and closing the {} in the wrong position!

WRONG

function quebraLampada() {
}

document.getElementById("Luz").src = "_imagens/lampada-quebrada.jpg ";

RIGHT

function quebraLampada() {
 document.getElementById("Luz").src = "_imagens/lampada-quebrada.jpg ";
}
    
28.10.2017 / 22:24