Uncaught TypeError: Cannot read property 'textContent' of undefined

when i click the date value i having this "error Uncaught TypeError: Cannot read property 'textContent' of undefined "

this is my code can you help me in determining the source of the error and how can i correct this

<?php
while($row = mysqli_fetch_array($query)){
                                    $date = $row['Date'];
                                    $time = $row['Time'];
                                    $latitude= $row['Latitude'];
                                    $longitude= $row['Longitude'];
                                    $depth =$row['Depth'];
                                    $magnitude = $row['Magnitude'];
                                    //$array_lat_lon[] = $lat = $row['LAT'];
                                    //$array_lat_lon[] = $lon = $row['LON'];

                                $the_arraypei[] = array($row['Date'] ); //added
                                //$the_array[] = array($row['LAT']."" , "".$row['LON']) ;      
                                //$timestamp = strtotime()
                                echo '<tr class="normalRow"><td id="date2"><a href="" onClick="functiontoget(\'$date\');">'.$date.'</a></td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';


                                                                }?>

   <script>
     function functiontoget($date) { 
     var x = document.getElementsByTagName("date")[0].textContent;
     document.getElementById("demo").innerHTML = x;}
     </script>
     <p id="demo"></p>


ANSWERS:


if you want to query elements by tag name you can use getElementsByTagName , but do you have a tag named date ? date is one element's id you need to use getElementById .

chage your javascript code :

function functiontoget($date) { 
   var x = document.getElementById("date").textContent;
   document.getElementById("demo").innerHTML = x;
}

This is my code.

The problem is that when I click the first value($date) in the table the alert is presented but when I click the other value($date) no alert is presented.

It is like the only clickable one is the first value($date)

<?php 
  while($row = mysqli_fetch_array($query)){
    $date = $row['Date'];
    $time = $row['Time'];
    $latitude= $row['Latitude'];
    $longitude= $row['Longitude'];
    $depth =$row['Depth'];
    $magnitude = $row['Magnitude'];
    echo '<tr class="normalRow"><td id="date2">'.$date.'</td><td border="1">'.$time.'</td><td border="1">'.$latitude.'</td><td border="1">'.$longitude.'</td><td border="1">'.$depth.'</td><td border="1">'.$magnitude.'</td></tr>';
  }}
  }
?>
<script>
  $(document).ready(function() {
    $("date2").click(function() {
      var x = $(this).text();
      alert(x);
      x = $(this).next().html();
      alert(x);
      ...
    });
  });
 </script>


 MORE:


 ? Vue Resource - Post multiple data
 ? Vue Resource - Post multiple data
 ? Vue Resource - Post multiple data
 ? Laravel 5 database query retrieve first table data but not in other table
 ? Laravel 5 database query retrieve first table data but not in other table
 ? Laravel 5 database query retrieve first table data but not in other table
 ? query join 3 tables on laravel
 ? How to convert mysql query to laravel 5?
 ? Laravel 5 query counting and grouping
 ? Build complex query in laravel 5