I am working on a website and trying to implement onClick function on a dynamically generated table.I am getting value from an HTML element using javascript(testBoot.php) and sending it to a pHp (table.php) to query DB based on the id and display result on testBoot.php in a Table. Now I want to add onClick function to the dynamically generated table row,I also tried jQuery dynamic binding and still unable to do the same
testBoot.php
< script type = "text/javascript" >
function showUser() {
var id = document.getElementById("dynamic_data").value;
if (id == "") {
document.getElementById("table-responsive").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("table-responsive").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "table.php?q=" + id, true);
xmlhttp.send();
}
} < /script>
<HTML>
<BODY>
<div class="table-responsive" id="table-responsive"></div>
<select name=city id="dynamic_data" class="form-control" onchange="showUser()"></select>
</BODY>
</HTML>
table.php
< script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" > < /script>
<script>
$('.clickable').on("click", function() {
$('#name').val($(this).children('td:first-child').text());
});
</script >
<body>
<?php $id=$ _GET[ 'q']; require 'data/connection.php'; // manage id echo "<input type = 'Hidden' id = 'testRunID' value = '".$id. "'>"; $sql=mysql_query( ""); if($sql==f alse){ die(mysql_error()); } echo "
<table id = 'myTable'> " ?>
<tr class='clickable'>
<?php echo "
<th>Area Name</th>
<th>Total Test Cases Executed</th>
<th>Passed</th>
<th>Failed</th>
<th>Others (Running/ Blocked/ Time Out)</th></tr>"; while($row=m ysql_fetch_array($sql)) { $testRunID=$ row[ 'areaName']; $totalCount1=$ row[ 'totalCount']; $passCount1=$ row[ 'passStatus']; $failCount1=$ row[ 'failStatus']; $otherCount=$
totalCount1 - ($passCount1 + $failCount1); ?>
<tr class="notfirst">
<?php echo "<td><a href='products.php?testRunID=".$id. "&areaName=" .$testRunID. "'>".$testRunID. "</a></td>"; //echo "<td>".$testRunID. "</td>"; echo "<td>".$totalCount1. "</td>"; echo "<td>".$passCount1. "</td>"; echo "<td>".$failCount1. "</td>"; echo
"<td>".$otherCount. "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($conn); ?>
<label for="name">Created by:</label>
<input id="name" type="text" />
</body>