I am developing a small web page.
On this page I have a table and created a function that takes the id and returns an image.
Everything is working correctly, the issue is that whenever I click on a line that function is executed.
I just wanted the function to execute when I click on the attached column of id's. (Second Column)
Does anyone can help me please?
Realized my doubts?
Thank you all.
<script>
function addRowHandlers() {
var table = document.getElementById("tableId");
var rows = table.getElementsByTagName("td");
for (i = 0; i < rows.length; i++) {
var currentRow = table.rows[i];
var createClickHandler =
function(row)
{
return function() {
var cell = row.getElementsByTagName("td")[1];
var id = cell.innerHTML;
//use ajax to post id to ajax.php file
$.post("ajax.php", { id:id }, function( data ) {
//add result to a div and create a modal/dialog popup similar to alert()
$('<div />').html(data).dialog();
});
};
};
currentRow.onclick = createClickHandler(currentRow);
}
}
window.onload = addRowHandlers();
</script>
table.rows[i].cells[1]createClickHandlerfunction on every loop iteration! Just create it once.var rows = table.getElementsByTagName("td");Um.... That'll give you everytd, not every row.