I am trying to do a simple table using html and jquery. What I am looking for is first of all hide one of the elements in this case is the element "notes", which I have done already. then using jquery I need to click in each type of animal and once clicked has to show the notes of each animal,one at a time. Which I have also done. My question is - to show the hidden "notes"one at a time, I have attributed a class for each type of animal - OK it is working but I do not think it is the more efficient way of doing it, because if I have 100 types of animals would be a waste of time attribute a class for each type of animal.
Can someone help me with that? here is my code:
<title> jQuery exercise</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
.clickable1,.clickable2 {
cursor:pointer;
text-decoration: none;
}
</style>
</head>
<body>
<table id="tableID">
<tbody>
<tr >
<th> Animal</th>
<td class="clickable1">Cat</td>
<td class="clickable2"> Dog</td>
</tr>
<tr>
<th> Notes</th>
<td class="toggle"> A</td>
<td class="toggle2"> C</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('.toggle,.toggle2').hide();
});
$(document).ready(function(){
$('.clickable1').click(function(){
$('.toggle').toggle(250);
});
});
$(document).ready(function(){
$('.clickable2').click(function(){
$('.toggle2').toggle(250);
});
});
</script>
</body>
</html>
Many thanks