Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On click slidetoggle div but hide others first - jQuery

I have a page that when a user clicks a title, the following div toggles display.

I want to somehow say if any other divs are display:block then set them to display none first.

I have the following...

$('.office-title').click(function(){
     $(this).next('div').slideToggle();
     return false;
});

and my html markup is as such...

<div class="office-row">
    <h3 class="office-title">Title</h3>
    <div class="office">sadasd</div>
</div>
<div class="office-row">
    <h3 class="office-title">Title</h3>
    <div class="office">sadasd</div>
</div>
<div class="office-row">
    <h3 class="office-title">Title</h3>
    <div class="office">sadasd</div>
</div>
like image 393
Liam Avatar asked Feb 12 '13 17:02

Liam


2 Answers

</office> is not a valid closing. The closing should be </div>

<div class="office-row">
        <h3 class="office-title">Title</h3>
        <div class="office">sadasd</div>
      </div>
    <div class="office-row">
        <h3 class="office-title">Title</h3>
        <div class="office">sadasd</div>
    </div>
    <div class="office-row">
        <h3 class="office-title">Title</h3>
        <div class="office">sadasd</div>
    </div>

CSS:

.office
{
    display: none;
}

and jquery:

$(function () {
    $('.office-title').click(function () {
        $(this).next('div').slideToggle();

        $(this).parent().siblings().children().next().slideUp();
        return false;
    });
});

HERE YOU CAN CHECK

like image 87
jubair Avatar answered Oct 13 '22 11:10

jubair


This should do it: http://jsfiddle.net/gKgJ7/

$('.office-title').click(function(){
   $('.office').slideUp();
   $(this).next('div').slideToggle();
   return false;
});

note:

This is not a valid closing:

<div class="office">sadasd</office>
   //---------------------^^^^^^^^----should be </div>
like image 22
Jai Avatar answered Oct 13 '22 11:10

Jai