Skip to main content
Participant
April 7, 2021
Question

Eventos de JavaScript en HTML5

  • April 7, 2021
  • 1 reply
  • 163 views

Buen día,

Cuando uso el evento getElementsByTagName no me esta ejecutando la instruccion:

document.getElementsByTagName("img")[0].onclick=tipoflor;

Donde:
función tipoflor(){
 alerta ("pensamientos");

}

Tampoco window.onload=daTipoFlor;

------------------------------------ Codigo ------------------------------------------
.
.
.

<title>Documento sin título</title>

<script>

función
tipoflor(){
alert("pensamientos");

}
función daTipoFlor(){
document.getElementsByTagName("img")[0].onclick=tipoflor;
}
window.onload=daTipoFlor;
</script>

</head>

<body> <p><img src="imagenes2/flor01.jpg"></p>

</body>
</html>

---------------------------------------------------------------------------------------

 

    This topic has been closed for replies.

    1 reply

    Legend
    April 7, 2021

     

    At a guess this may be what you are looking for:

     

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Onclick Function</title>
    </head>
    <body>
    
    <p><img src="images2/flor01.jpg"></p>
    
    <script>
    function typeflor() {
    alert("thoughts");
    }
    
    function daTypeFlor() {
    document.getElementsByTagName("img")[0].onclick = function() {
    // call the typeflor() function when image is clicked
    typeflor();
    }
    }
    
    // run the daTypeFlor() function on window load
    window.onload = function() {
    daTypeFlor();
    };
    </script>
    	
    </body>
    </html>