how do I take the id of a tag and then save it in a javascript variable

1
<html>
<head>

</head>
<body>
<?php
$user="localhost";
$username="root";
$pass="";
$db="despensa";
$conexion=mysqli_connect($user,$username,$pass,$db)or die  ("ERROR DE CONEXION");
                            $consultar="select * from cliente ";
                            $resultado=$conexion->query($consultar);
                            while ($resultados=$resultado->fetch_assoc()){
                                ?>

                                <?php $a=$resultados['email'];?> 
                                <?php $b=$resultados['idcliente'];?> 
                        <ul>
                                <li><a id="<?php echo $a?>" href="#" onclick="name();"><?php echo $a?></a></li>


                                </ul>

                                <script type="text/javascript">
                                function name() {
                                    var name = document.getElementById("<?php echo $a?>").value;
                                    alert(name);
                                }
                                </script>           
                    <?php }?>



</body>
</html>

    
asked by Leonardo Mancero 16.09.2017 в 16:58
source

3 answers

1

When the method onclick is executed, that method is executed on the context of the element, which means that the onclick , interprets that name is the attribute , not a function .

If you execute this code, you will notice how the value of the attribute name prevails:

  <button onclick="alert(name)" name="hola">
   imprimir
  </button>
    

So by telling you that name is not a function it is correct because it is an attribute.

Try accessing the function using the window object, which is where all the global functions belong. For example:

function name(){
  console.log("hola mundo");
}
 <button onclick="window.name()" name="hola">
       imprimir
      </button>

Or rename your function to any other name:

//..
while ($resultados=$resultado->fetch_assoc()){
    ?>

    <?php $a=$resultados['email'];?> 
    <?php $b=$resultados['idcliente'];?> 
    <ul>
        <li><a id="<?php echo $a?>" href="#" onclick="nombre();"><?php echo $a?></a></li>
    </ul>

    <script type="text/javascript">
        function nombre() {
            var name = document.getElementById("<?php echo $a?>").innerHTML;
            alert(name);
        }
        </script>           
<?php }?>

And another thing. You are generating a function name() for each iteration that the while does. Which means that if the while iterates 5 times, 5 functions with the same name will be created and only the last one will be taken as a reference, not the closest to the click event.

For example:

<?php

 for($i = 0; $i < 5; $i++)
 {
     ?>
    <button onclick="namef()" name="hola">
     imprimir
    </button>
      <input type="text" id="input_<?php echo $i ?>" value="<?php echo $i ?>"></input>


   <script type="text/javascript">
      function namef() {
          var f = document.getElementById("input_<?php echo $i?>").value;
          alert(f);
      }
     </script>   
<?php
 }

?>

Regardless of which button you click, it will always print 4. Try to get the definition of the function out of while and send the id of the element you want the parameter value to the function when the event is done click like this:

<?php

 for($i = 0; $i < 5; $i++)
 {
     ?>
    <button onclick="namef('input_<?php echo $i ?>')" name="hola">
     imprimir
    </button>
      <input type="text" id="input_<?php echo $i ?>" value="<?php echo $i ?>"></input>

<?php
 }

?>
   <script type="text/javascript">
      function namef(id) {
          var f = document.getElementById(id).value;
          alert(f);
      }
     </script>  
    
answered by 16.09.2017 в 23:45
0

the element does not contain the value property, only the inputs have that property, unless the assignment (which is not the case) changes the function name to:


function name(){
var name = document.getElementById("").innerHtml;
alert(name);
}

this will show the content of the anchor.

    
answered by 16.09.2017 в 21:16
0

As you Einer tell you, you're recreating the same function for each of the while cycles, you should check out the loop as well as the

    element, and placed on the line that you define the link < a > I would pass to the function as a parameter the element itself by using the keyword this instead of passing its' id ':

    <li><a id="<?php echo $a?>" href="#" onclick="name(this);"><?php echo $a?></a></li>
    

    ... and then act accordingly to collect it in the function 'name' and take advantage to perform a simple check ...

    function name(elemento) {
        if (elemento) { alert(elemento.innerHTML); }
    }

    ... so you have a few options to implement it but GOD CHANGE THE NAME TO THE FUNCTION! jejjejj

        
answered by 18.09.2017 в 03:49