1

Is there any way to show cell values from html table to textboxes when a specific row was clicked ?

          $result=mysqli_query($conn, $sql1);
          echo "<table class='data'>
              <tr>
                  <th>Agent ID</th>
                  <th>Fullname</th>
                  <th>Address</th>
                  <th>Gender</th>
                  <th>Contact</th>

              </tr>";

          while($row=mysqli_fetch_assoc($result))
          {
              echo "<tr>";
              echo "<td>". $row['id']. "</td>";
              echo "<td>". $row['fullname']. "</td>";
              echo "<td>". $row['address']. "</td>";
              echo "<td>". $row['gender']. "</td>";
              echo "<td>". $row['contact']. "</td>";
              echo "</tr>";
          }  
          echo "</table>";
  ?>

user5567987
  • 167
  • 1
  • 12
  • http://stackoverflow.com/questions/376081/how-to-get-a-table-cell-value-using-jquery – Mahi Nov 20 '16 at 16:25

1 Answers1

0

You could use jQuery to do this with the on("click") event handler.

(see comments)

$("td").on("click", function() {

  /* create a new input element */

  var new_edit = document.createElement("input");
  $(new_edit).prop("type", "text");

  /* set value to whatever was in <td>  */
  $(new_edit).val($(this).text()); 

  /* insert new input into <td>  and focus on the new input */
  $(this).html(new_edit);

  $(new_edit).focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="50%" border="1px solid black">
  <th>col 1</th>
  <th>col 2</th>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>

</table>
mike510a
  • 2,102
  • 1
  • 11
  • 27