Jed
Jed

Reputation: 1733

All elements with the same class are affected by click function

How can I modify my code that affects all the elements with the same class In my Jquery to make click effects only to my desired html element (.divCollapsilble)

This is my site that shows the effect of my code below

My script.js:

// JavaScript Document
  $(document).ready(function(){
  $('.divCollapsible').hide();
  $('.divToggle').click(function(){
  var location = $(this).parent().parent();

    $('.divCollapsible').slideToggle('slow');
  });
});

How can I implement

Upvotes: 1

Views: 2427

Answers (2)

Sowmya
Sowmya

Reputation: 26969

Change the code like this

$(document).ready(function(){
  $('.divCollapsible').hide();
  $('.divToggle').click(function(){
    $(this).next(".divCollapsible").slideToggle('slow');
  });
});

DEMO

Upvotes: 5

Simon
Simon

Reputation: 3717

$(this).siblings('.divCollapsible').slideToggle('slow'); should do. That way you are targeting only siblings of your .divToggle with class .divCollapsible instead of all collapsible elements on the page.

Upvotes: 3

Related Questions