Is there a possibility to toggleText() for each element of the array separately?
Right now, when clicked, the values are changed for every single element at the same time and I'd like to go over them one by one as they're clicked.
$(function() {
var jap = ["kau", "matsu", "shiru"];
var eng = ["to buy", "to wait", "to know"];
var tds = [$('#td1'), $('#td2'), $('#td3')]
for (i = 0; i < tds.length; i++) {
$(tds[i]).on('click', function(){
for(i = 0; i < tds.length; i++) {
$(tds[i]).toggleText(jap[i], eng[i]);
}
});
}
});
Ideally, I'd like to change the text inside a table cell on each individual click for each array element.
-- edit:
Here's the HTML with the table:
<table>
<tbody id="tb">
<tr>
<td>Godan Verbs</td>
<td>V2</td>
<td>Ichidan Verbs</td>
<td>V2</td>
<td>Irregular Verbs</td>
<td>V2</td>
</tr>
<tr>
<td>kau</td>
<td>kai-</td>
<td>taberu</td>
<td>tabe-</td>
<td>kuru</td>
<td>ki-</td>
<tr>
<td>matsu</td>
<td>machi-</td>
<td>miru</td>
<td>mi-</td>
<td>suru</td>
<td>shi-</td>
</tr>
<tr>
<td>shiru</td>
<td>shiri-</td>
<td>oshieru</td>
<td>oshie-</td>
</tr>
<tr>
<td>kaku</td>
<td>kaki-</td>
<td>neru</td>
<td>ne-</td>
</tr>
<tr>
<td>oyogu</td>
<td>oyogi-</td>
<td>kangaeru</td>
<td>kangae-</td>
</tr>
<tr>
<td>hanasu</td>
<td>hanashi-</td>
<td>okiru</td>
<td>oki-</td>
</tr>
<tr>
<td>shinu</td>
<td>shini-</td>
<td>sakeru</td>
<td>sake-</td>
</tr>
<tr>
<td>yomu</td>
<td>yomi-</td>
<td>kotaeru</td>
<td>kotae-</td>
</tr>
<tr>
<td>asobu</td>
<td>asobi-</td>
<td>iru</td>
<td>i-</td>
</tr>
</tbody>
</table>
Here's additional meaning of the words I'd like to display:
const jap2en = {"kau":"to buy", "matsu":"to wait", "shiru":"to know", "kaku":"to write", "oyogu":"to swim", "hanasu":"to talk", "shinu":"to die", "yomu":"to read", "asobu":"to play", "taberu": "to eat", "miru": "to look", "oshieru": "to teach", "neru": "to sleep", "kangaeru": "to think", "okiru": "to get up", "sakeru": "to avoid", "kotaeru": "to answer", "iru": "to be/exist", "kuru": "to come", "suru": "to do"};
I also added ids to the tds that are supposed to show the meaning like this for each individual td:
<td id="td1">kau</td>
<td>kai-</td>
<td id="td2">taberu</td>
// and so on
and changed the js code to this:
const $tds = $("#tb td[id]");
$("#tb").on("click", "td[id]", function() { ... }
However, as I mentioned in the comment, that one fragment changes the structure of the table and makes tds appear in wrong places. This fragment specifically:
$tds.each(function(i) {
$(this).text(jpArr[i])
$(this).data("lang","ja")
})
If removed, the code works but the cells require two clicks to show the meaning.