最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add click event to table cell in this code? - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question edited Jun 2, 2016 at 12:37 srikanth_k asked Jun 2, 2016 at 10:51 srikanth_ksrikanth_k 2,9173 gold badges17 silver badges18 bronze badges 1
  • Possible duplicate of Javascript onClick event in all cells – John Commented Jun 2, 2016 at 11:03
Add a comment  | 

5 Answers 5

Reset to default 9

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.

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

table.addEventListener("click", function(e) {
  if (e.target && e.target.nodeName == "TD") {
    alert('Cell clicked')
  }
});

After the cell is added to the DOM you can add eventListeners to it. So the only thing you have to do, is add the eventListener :)

// here you add the cell and have a reference to it
cell = row.insertCell(j);

// now you can add eventlisteners to it
cell.addEventListener('click', function(){
    console.log('cell clicked');
});

About adding event listeners: MDN

Demo: https://jsfiddle.net/kt9g8h4w/1/

Add a click listener first and write a function with same listener name which will be executed after clicked. Try the code below:

<!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);
      row.addEventListener('click', clickHandlerRow);
      for(var j=0;j<2;j++){
            cell = row.insertCell(j);
            cell.addEventListener('click', clickHandlerColumn);
        }
    }
}

function clickHandlerRow(e) {
    alert("row clicked");
}

function clickHandlerColumn(e) {
    alert("column clicked");
}
</script>
</head>
<body onload="clickHere()">

<table id = "myTable" border="1"></table>

</body>
</html>

$("#myInput").val($(This).children().eq(0).text()); - above code (1) 'myInput' is id of input box where we display the selected table cell value. (2)'eq(0)'-> which denote ,cell position value display in required in input box.

发布评论

评论列表(0)

  1. 暂无评论