I am fairly new to web development and to get hang of doing simple tasks I am working on a project and it turns out for a task I want to carry out I have to use Javascript but having only little knowledge of Javascript I can't figure out how to do what I want and I have searched online but haven't been able to finding anything of use:
How to know element clicked within a table?
How to access specific cell of clicked table row in javascript
...
Here is the project code I have written so far:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Menu</title>
<script>
function toggleTable() {
var lTable = document.getElementById("menulist");
lTable.style.display = (lTable.style.display == "table") ? "none" : "table";
}
</script>
</head>
<body>
<table width="300" border="1" id="menuitems">
<tbody>
<tr>
<td onclick="toggleTable()" id="cell1"> On-Menu</td>
<td onclick="toggleTable()" id="cell2"> On-Menu</td>
<td onclick="toggleTable()" id="cell3"> On-Menu</td>
<td onclick="toggleTable()" id="cell4"> On-Menu</td>
</tr>
<tr>
<td onclick="toggleTable()" id="cell5"> On-Menu</td>
<td onclick="toggleTable()" id="cell6"> On-Menu</td>
<td onclick="toggleTable()" id="cell7"> On-Menu</td>
<td onclick="toggleTable()" id="cell8"> On-Menu</td>
</tr>
<tr>
<td onclick="toggleTable()" id="cell9"> On-Menu</td>
<td onclick="toggleTable()" id="cell10"> On-Menu</td>
<td onclick="toggleTable()" id="cell11"> On-Menu</td>
<td onclick="toggleTable()" id="cell12"> On-Menu</td>
</tr>
</tbody>
</table>
<table width="300" border="1" id="menulist">
<tbody>
<tr>
<td id="cellorange"> Orange</td>
</tr>
<tr>
<td id="cellapple"> Apple</td>
</tr>
<tr>
<td id="cellbanana"> Banana</td>
</tr>
</tbody>
</table>
</body>
</html>
here I have 2 sets of table, 1 containing empty cells respresented by 'On-Menu' and another containing list of options. Please have a look here: https://jsfiddle.net/pz6tc3ae/
The project so far, when clicked on each table cell triggers a javascript function toggleTable(), which toggles the section table called menulist containing, Orange, Apple and Banana.
Table 1 = menuitems Table 2 = menulist
What I want to know is:
The user can click on any cell within Table 1, which will trigger the toggleTable function and toggle (show/hide) table 2. What I want to know is which table cell triggered table 2.
Having the first task done, within table 2, I have given ids to each of the three options orange, apple and banana, when user clicks on any of these options I want the text of this cell to be written on the cell from table 1 that triggered table 2 in first place.
I have no clue on how I can go about doining this and having little knowledge of Javascript, my research haven't proved to as effective as I wanted it to be, therefore I would really appreciate if someone can help me out.
Thanks in advance and if my question was not clear enough please let me know.