0

I have the following two JavaScript function:

JS 1

$( ".show-more" ).click(function() {
  event.preventDefault();
  $( this ).next().slideToggle( "fast", function() {
  });
  $( this ).toggleClass("show-more-rotate");
});

JS 2

$( ".show-more-section" ).click(function() {
  event.preventDefault();
  $( this ).next().slideToggle( "fast", function() {
  });
  $( this ).toggleClass("show-more-section-rotate");
});

Is there a way to concatenate the two functions into one? I tried the following, but the functionality seems to only be working for the last listed element:

JS - Failed attemp at concatenating

$( ".show-more, .show-more-section" ).click(function() {
  event.preventDefault();
  $( this ).next().slideToggle( "fast", function() {
  });
  $( this ).toggleClass("show-more-rotate, show-more-section-rotate");
});
3
  • You can create the callback function and reference it Commented Jun 17, 2015 at 9:32
  • 5
    Remove comma from toggleClass Commented Jun 17, 2015 at 9:33
  • Shouldn't your call to toggleClass() be part of the callback passed to slideToggle() instead of running sequentially? Commented Jun 17, 2015 at 9:36

4 Answers 4

2

See comments inline:

// bind event on both the elements
$(".show-more, .show-more-section").on('click', function (event) {
//                                                        ^^^^^^ Add this
    event.preventDefault();

    $(this).next().slideToggle("fast", function () {});
    $(this).toggleClass("show-more-rotate show-more-section-rotate");
    // Remove `,` from toggleClass
});

EDIT

You can also chain the methods as:

$(this).toggleClass("show-more-rotate show-more-section-rotate")
    .next().slideToggle("fast", function () {});

Update

If you want to toggle class after slideToggle is completed:

var $this = $(this);
$this.next().slideToggle("fast", function () {
    $this.toggleClass("show-more-rotate show-more-section-rotate")
});
Sign up to request clarification or add additional context in comments.

4 Comments

The original answer you gave seems to only be targeting and working for .show-more-section and .show-more-section-rotate, therefore only the last listed elements.
@JulianSamarjiev What are you saying?
@Tushar I am getting the same result as the code I listed under my failed attempt (only the second class' are getting targeted), and yes I did remove the comma :)
@JulianSamarjiev Please create jsfiddle for this
1

try this:-

 $(".show-more, .show-more-section").on('click', function (event) {
   event.preventDefault();
   $(this).next().slideToggle("fast", function () {});
   if($(this).is('.show-more'))
   {
    $(this).toggleClass("show-more-rotate");       
   }else
   {
    $(this).toggleClass("show-more-section-rotate");
   }
});

Demo

Comments

1

You can create the handler function and can reuse it in the click handler

$( ".show-more" ).click(showMoreClickHandler);

$( ".show-more-section" ).click(showMoreClickHandler);

function showMoreClickHandler(e) {
    e.preventDefault();
    $( this ).next().slideToggle( "fast", function() {});
    $( this ).toggleClass("");
}

For toggle class, you can add data-toggle-class attribute in HTML and can read the value and perform toggling.

I hope, show-more-rotate and show-more-section-rotate should be applied on it's respective element not on the other.

1 Comment

You are forgetting one class to toggle
0

Remove the comma in the toggleClass:

$(".show-more, .show-more-section").on('click', function (event) {
    event.preventDefault();
    $(this).next().slideToggle("fast", function () {
    });
    $(this).toggleClass("show-more-rotate show-more-section-rotate");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.