Javascript - Onclick in links

0

My question is: can onclick events be done on links?

cambiar_estado.php

$query =    "SELECT 
                *   
            FROM
                usuarios_datos
            WHERE 
                ID_OBLIGATORIO = '".$_REQUEST["Id"]."'";

    $result = mysql_query($query);
    //Mientras existen datos, analizamos el ID asignado extrayendo su estado
    while($row = mysql_fetch_array($result)){
    ?>
        <center>
        <?php 
            if($row["estado"] == 0) {
                echo "<a href=\"javascript:cargaXML('cambiar_estado.php?Id=".$_REQUEST["Id"]."','estado".$_REQUEST["Id"]."')\">";
                    echo "<img src=\"/imagenes/0.gif\">";
                echo "</a>";
            } else if($row["estado"] == 1) {
                echo "<a href=\"javascript:cargaXML('cambiar_estado.php?Id=".$_REQUEST["Id"]."','estado".$_REQUEST["Id"]."')\">";
                    echo "<img src=\"/imagenes/1.gif\">";
                echo "</a>";
            } else {
                echo "<a href=\"javascript:cargaXML('cambiar_estado.php?Id=".$_REQUEST["Id"]."','estado".$_REQUEST["Id"]."')\">";
                    echo "<img src=\"/imagenes/2.gif\">";
                echo "</a>";
            }
        ?>
        </center>
    <?php 
    }

    mysql_free_result($result);

index.php

echo "<td width=\"08%\" id=\"estado".$row['ID_OBLIGATORIO']."\">" ?>
    <!-- Columna ESTADO del usuario.    -->
    <center>
    <?php
        $estado = 1;
        "<a href=\"javascript:cargaXML('cambiar_estado.php?Id=".$row['ID_OBLIGATORIO']."','estado".$row['ID_OBLIGATORIO']."')\">";
              echo "<img src=\"/imagenes/".$row['estado'].".gif\">";
        echo "</a>";
    ?>
    </center>
    <?php 
echo "</td>";

Well my question is, in those links I can insert an onclick event so that when I click on the image .gif ask me (confirm ()) if I want to show imagen1.gif or imagen2.gif? I hope you have explained to me.

    
asked by omaza1990 15.11.2016 в 17:09
source

1 answer

2

function doClick(e){
	e.preventDefault();
	alert(this.innerHTML);
}
var aTags =  document.querySelectorAll('a');

for(var i = 0; i < aTags.length; i++){
    aTags[i].addEventListener('click',doClick,false);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<a href=""> click me</a>
  <a href=""> click me too</a>
</body>
</html>

If you can, one way to do it is to add a Listener to the tags

the Listener passes an Event as an argument, the key is in e.preventDefault() since it avoids the normal behavior of the tag

    
answered by 15.11.2016 в 18:59