Using jQuery I am trying to create a function to show and hide the DIV below the table/tr that has been clicked, but when I click in one, all the DIVs will slide up, also slide down it is not working.
I tried if ($(".container:first").is("hidden")) but I guess is not the first element under element clicked but first element on the document.
How do I get this working?
Based on the following example.
<html>
<head>
<script type="text/javascript" src="jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".header").click(function(){
if ($(".container").is("hidden")){
$(".container").slideUp("slow");
} else {
$(".container").slideDown("slow");
}
});
});
</script>
</head>
<body>
<table class="header">
<tr><td>Row1</td></tr>
</table>
<div class="container">Container 1</div>
<table class="header">
<tr><td>Row2</td></tr>
</table>
<div class="container">Container 2</div>
</body>
</html>
You want to get only the next container, so you’d use
$.next():Online Demo: http://jsbin.com/eyuwi3/edit
Updated
Requested in the comments was to make this "work on the
trelements." If you click anywhere within the table, the table’s click event will eventually fire – unless a child is stopping the propagation.If this code is not working, then your problem is elsewhere. Make sure you don’t have elements between your table and your
.containerelement.$.next()only selects immediate siblings, which is what your sample-code suggests you have.If problems persist, you could try the following:
Online Demo: http://jsbin.com/eyuwi3/2/edit