Hi i want when the one #box is open the others #box close.
Html:
<div id="main">
<div id="link">click</div><!--/*div*/-->
<div id="box">content1</div><!--/*div*/-->
</div><!--/*div*/-->
<div id="main">
<div id="link">click2</div><!--/*div*/-->
<div id="box">content2</div><!--/*div*/-->
</div><!--/*div*/-->
<div id="main">
<div id="link">click3</div><!--/*div*/-->
<div id="box">content3</div><!--/*div*/-->
</div><!--/*div*/-->
Jquery:
<script type="text/javascript">
$(document).ready(function(){
$('#main #box').css('display', 'none')
$('#main #link').click(function() {
$(this).next('#main #box').slideToggle('slow')
.siblings('#main #box:visible').css('display', 'none');
});
});
</script>
Thanks!
IDs should only be one per page.
You shouldn’t reuse “main”.
Rather, do something like this:
Then, you would change your javascript like so:
Remember, you’re dealing with the siblings of the
.box, so you don’t need to specify .main again. You’re essentially looking for siblings of main which are calledclass='box:visible'I might suggest, that unless you really need to, you probably don’t need the
mainclass. Rather, structure your code like so:And then, your javascript (edited) like so:
The reason for this is two-fold. One, IDs are faster to find in the DOM and it gives a structure to your document. Because you’re not supposed to reuse IDs, you will only have ONE main element on your page.
However, without seeing the rest of your code, I can’t guarantee that this will work ( or break ) your layout.