jquery - slideToggle on multiple items, if one is open close that one before opening the other -


i have little bit of js done, simple stuff know wondering best way if expanded 1 , click on expand open close before other 1 opened.

js

/* expand course details */ $('.expandcourse').on('click',function() {   var courseid = $(this).attr('id');   console.log(courseid);   $("."+courseid).slidetoggle();   return false; }); 

html example:

<a href="#" id="link_1" class"expandcourse">test link 1</a> <div class="link_1" style="display:none;>content here</div>  <a href="#" id="link_2" class"expandcourse">test link 2</a> <div class="link_2" style="display:none;>content here</div>  <a href="#" id="link_3" class"expandcourse">test link 3</a> <div class="link_3" style="display:none;>content here</div> 

thanks in advance!

you can use this

$('.expandcourse').on('click',function() {    $("[class^=link]").not($(this).next()).slideup();    $(this).next().slidetoggle(); }); 

demo

in each click hides divs classname starts link except div related current anchor.

edit

$('.expandcourse').on('click',function() {    $("[class^=link]").not($("."+this.id)).slideup();   $("."+this.id).slidetoggle(); }); 

updated fiddle


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -