I have a simple html table where the rows are dynamically created and values are inserted into the cells. This is the code:
var table=document.getElementById("emp");
var row=table.insertRow(0);
var cell = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
cell.innerHTML="<b>Name:</b>";
cell1.innerHTML="<b>Age:</b>";
cell2.innerHTML="<b>Address:</b>";
var row1=table.insertRow(1);
var cell3=row.insertCell(0);
var cell4=row.insertCell(1);
var cell5=row.insertCell(2);
cell3.innerHTML="Alex";
cell4.innerHTML=23;
cell5.innerHTML="Chicago";
Now, I want to be able to select multiple rows in my table, using the Ctrl or Shift. How do I do that? Please help me.
You can check inside your click handler if a key is pressed and if so, perform the appropriate action
const trs = document.querySelectorAll('tr');
trs.forEach(tr => tr.addEventListener('click', (e) => {
console.log(e.shiftKey); // Will print true if shift is pressed.
}))