0

I'm trying to get a line of a dynamic table once the " reserver" button is clicked (or the line itself is clicked 'if that matters')but the 'getElementById' doesn't return anything ( i want to display them in an input) here is the code

HTML / PHP

<div class="form1">
            <div>
            <label>Code creneau1</label>
            <input type="text" id="nop" name='nop'>
            <label>Code creneau2</label>
            <input type="text" id="seh" name='seh'>
            <label>Code creneau3</label>
            <input type="text" id="row" name='row'>
            <label>Code creneau4</label>
            <input type="text" id="name" name='name'>
            
            </div>
        <table class="table table-striped" id="res">
        <thead>
            <tr>
                <th>code salle</th>
                <th> type </th>
                <th>etage</th>
                <th>capacité</th>
                <th> action</th>
                
                
            </tr>
        </thead> 
 <tbody><?php
        while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
                 {
                    $donnees_ar=$_POST['date'];
                    $code_cr=$_POST['heure'];
                // echo $donnees2['id_res'].'<br>';
              
            
                 if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
 echo "<tr id='try'>

                <td >".$donnees['code_salle']."</td>
                <td >".$donnees['type_salle']."</td>
                <td >".$donnees['capacity']."</td>
                <td >".$donnees['etage']."</td>
                
                <td>
                  <a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
                </td> 
            </tr>";
          }
        }; 
      }
         ?>

the JS

    <script src="js/main.js"></script>
   <script>
        var tbl= document.getElementById("res");
        for(var x=1;x<tbl.rows.length;x++ ){
            tbl.rows[x].onclick =function {
               document.getElementById('name').value=this.cells[0].innerHTML;
               document.getElementById('row').value=this.cells[1].innerHTML;
               document.getElementById('seh').value=this.cells[2].innerHTML;
               document.getElementById("nop").value=this.cells[3].innerHTML;

            }
        }
    </script>
1
  • 1
    "'getElementById' doesn't return anyhting" Not possible, the said method returns either a reference to an element or null. Make sure not to run the code before the elements exist, and that this in your code refers to a correct element (I doubt it isn't).
    – Teemu
    May 4, 2022 at 10:36

1 Answer 1

0

You have been declared the JavaScript inside the JS file itself. in this case, The JS file Won't run for your HTML and won't affect anything written in your HTML. Also, you have declared your onClick function in the wrong way. To do this, you need to move the tag inside your HTML. For a better demonstration:

var table= document.getElementById("res");

if (table != null) {
    for (var i = 0; i < table.rows.length; i++) {
        for (var j = 0; j < table.rows[i].cells.length; j++)
        table.rows[i].cells[j].onclick = function () {
            tableText(this);
        };
    }
}

function tableText(tableCell) {
    alert(tableCell.innerHTML);
}
<div class="form1">
            <div>
            <label>Code creneau1</label>
            <input type="text" id="nop" name='nop'>
            <label>Code creneau2</label>
            <input type="text" id="seh" name='seh'>
            <label>Code creneau3</label>
            <input type="text" id="row" name='row'>
            <label>Code creneau4</label>
            <input type="text" id="name" name='name'>
            
            </div>
        <table class="table table-striped" id="res">
        <thead>
            <tr>
                <th>code salle</th>
                <th> type </th>
                <th>etage</th>
                <th>capacité</th>
                <th> action</th>
                
                
            </tr>
        </thead> 
 <tbody>
 <?php
        while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
                 {
                    $donnees_ar=$_POST['date'];
                    $code_cr=$_POST['heure'];
                // echo $donnees2['id_res'].'<br>';
              
            
                 if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
 echo "<tr id='try'>

                <td >".$donnees['code_salle']."</td>
                <td >".$donnees['type_salle']."</td>
                <td >".$donnees['capacity']."</td>
                <td >".$donnees['etage']."</td>
                
                <td>
                  <a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
                </td> 
            </tr>";
          }
        }; 
      }
         ?>

4
  • My JS is in the HTML file and is the last thing on the page ( it's not in a separate file) and i don't click on the line until the html and php R done and still nothing is working @ShayanFaghani May 4, 2022 at 10:53
  • Dear @OumniaRanaSEGUENI I don't know about the exact functionality, but if you need to change something you can use the above-edited code. It will alert the cell that has been clicked. You can add your own functionality to it as you desire. May 4, 2022 at 13:47
  • Was it a solution for you? May 5, 2022 at 13:49
  • thank you it worked ! although now i'm stuck trying to transfer it form Js to php but your code worked so thanks again May 5, 2022 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.