rachel
rachel

Reputation: 157

Combining jQuery on click

I found this http://jsfiddle.net/Stocki/jGbhh/

and changed to this:

$(document).on("click", ".slides", function () {
    $(".slide").removeClass("inside");
    $("#" + this.id + "_slide").addClass("inside");
});

$(document).on("click", ".close", function () {
    $(".slide").removeClass("inside");
});

It works perfectly, but it doesn't seem right. I feel it should be more of a on click .slides or .close then removeClass else on click addClass.

Any help would be appreciated. This is my first attempt and I'd really like to understand why/how it works and how that should be written so I'm not using $(document) twice.

Upvotes: 0

Views: 64

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try using a multiple selector and split up the flow inside the event handler by means of the class that the current element is having,

$(document).on("click", ".slides,.close", function(){
  $(".slide").removeClass("inside");
  if($(this).hasClass('slides')){
     $("#"+this.id+"_slide").addClass("inside");
  }
});

Upvotes: 3

Related Questions