I currently have a javascript function that acts on a table. When you click on a table cell in a table row all the other cells disappear and the clicked cell gets moved to the leftmost of the table row and there will be a slider opening up on the right side of the table cell. I want to make each table cell’s slider unique so I’m just wondering if there’s a way to make a function’s name a string plus a variable. For example if the clicked cell has a id of cell1 can I do something like let id = the id of the table cell and when I’m doing the function for the slider I name it ‘slider’ + id?
js
function handleCellClick(clickedCell) {
const row = clickedCell.parentElement;
const cells = row.children;
const display = cells[cells.length - 1].style.display ? '' : 'none';
for (let i = 0; i < cells.length; i++) {
cells[i].style.display = display;
}
if (display) {
const newCell = clickedCell.cloneNode(true);
newCell.style.display = '';
newCell.colSpan = cells.length;
row.insertBefore(newCell, row.firstChild);
openSlider(newCell);
} else {
clickedCell.remove();
closeSlider(clickedCell);
}
}
HTML
<table id='TrendGames'>
<tbody>
<tr>
<td id="cell1">
<img class='GameCover' src='.png' alt="cell1" />
</td>
<td id="cell2">
<img class='GameCover' src='.png' alt="cell2" />
</td>
<td id="cell3">
<img class='GameCover' src='.png' alt="cell3" />
</td>
<td id="cell4">
<img class='GameCover' src='.png' alt="cell4" />
</td>
</tr>
</tbody>
</table>
Any help is appreciated!
2
Answers
Ok I worked it out by pulling out the OpenSlider function and defining each unique slider there.
Have you tried using "this" as parameter for your function ?
HTML
SCRIPT
I did not understand the logic entirely but using "this" as parameter you can use same function for different elements.
And yes you can change cloned nodes id whatever you like.