Currently I can only show/hide a single element like so:
$(document).ready(function () {
$("#Mostrar_Tabla").click(function () {
if ($("#Tabla_Mostrar").is(":visible")) {
document.getElementById("Tabla_Mostrar").style.display = 'none';
}
else {
document.getElementById("Tabla_Mostrar").style.display = '';
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tr id="Mostrar_Tabla">
<td>
Dale Click Aqui
</td>
</tr>
<tr id="Tabla_Mostrar">
<td>
<table class="table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
</table>
But I need to make it work with n elements tr
. How would I have to modify my script to work with html like this:
<table class="table">
<tr id="Mostrar_Tabla1">
<td>
Dale Click Aqui
</td>
</tr>
<tr id="Tabla_Mostrar1">
<td>
<table class="table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
<tr id="Mostrar_Tabla2">
<td>
Dale Click Aqui
</td>
</tr>
<tr id="Tabla_Mostrar2">
<td>
<table class="table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
</tr>
<tr id="Mostrar_Tabla3">
<td>
Dale Click Aqui
</td>
</tr>
<tr id="Tabla_Mostrar3">
<td>
<table class="table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
</td>
</tr>
</table>
Assign each
tr
one you click a class (the same for all):In the JavaScript code, declare the event
click
on elements with that class. In this event, you show or hide the onetr
whose id starts withTabla_Mostrar
that followstr
the one you clicked:Alternatively, if you can't or prefer not to add the class to these
tr
, you can have the event affecttr
whoseid
starts withMostrar_Tabla
:I hope this helps you
Cheers!
It also stayed this way, which is a little simpler:
I have changed them
id
toclass
only thetr
one where it will be clicked and I have made usenext()
of jQuery leaving the script in this way:What it does
$(this).next()
is select the next sibling of$('.Mostrar_Tabla')
which it would be<tr id="Tabla_Mostrar1">
Example: