In this code, I want to make the table cell clickable with javascript.
Please also tell me how to use i,j values with the click event.
<!DOCTYPE html>
<html>
<head>
<style>
td
{
height : 30px;
width : 30px;
cursor: pointer;
}
</style>
<script>
function clickHere(){
var table = document.getElementById("myTable");
var row ;
var cell;
for(var i=0;i<2;i++){
row = table.insertRow(i);
for(var j=0;j<2;j++){
cell = row.insertCell(j);
}
}
}
</script>
</head>
<body onload="clickHere()">
<table id = "myTable" border="1"></table>
</body>
</html>
Add this code:
cell.addEventListener("click",function(){
alert("cell clicked");
});
After this code:
cell = row.insertCell(j);
It will add event listener to each cell. when clicked it will show an alert.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments