-1

I'm looking for a way to add and remove class on the same button. So far this is my work in progress. The concept is when I click on the menu button it shows the menu. When I tap on the menu button again. The menu hides

$(document).ready(function(){
    $('button.toggle-portfolio').on('click', function(e){
      $('.portfolio-contact-form-wrap').addClass('show');
    });
});
4
  • 3
    $('button.toggle-portfolio').on('click', function(e){ $(this).toggleClass('show'); }); try this on same button? Commented Feb 4, 2016 at 10:26
  • 1
    It is really unclear what you are asking... At least, provide relevant HTML markup and better describe expected behaviour Commented Feb 4, 2016 at 10:37
  • Seriously, Please Use Google..... Commented Feb 4, 2016 at 10:43
  • Possible duplicate of stackoverflow.com/q/19520446/5620297 Commented Feb 4, 2016 at 10:45

7 Answers 7

2

To achieve this you can use .toggleClass() like so:

$(document).ready(function(){
    $('button.toggle-portfolio').on('click', function(e){
      $('.portfolio-contact-form-wrap').toggleClass('show');
    });
});

JsFiddle example


toggleClass

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.

This method takes one or more class names as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added.

For more information about this function check here Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

You should use $(this) and toggleClass

$(document).ready(function(){
    $('button.toggle-portfolio').on('click', function(e){
      $(this).toggleClass('show');
    });
});

which will add the class back to the specific element that was clicked.

http://api.jquery.com/toggleclass/

http://www.learningjquery.com/2007/08/what-is-this

Comments

1

Try this:

$(document).ready(function(){
  $('button.toggle-portfolio').on('click', function(e){
     $('.portfolio-contact-form-wrap').toggleClass('show');
  });
});

Comments

1

DEMO

$('.toggle-portfolio').on('click', function(e) {
  $('.portfolio-contact-form-wrap').toggleClass('show');
});

Try this way

2 Comments

OP doesn't want to toggle class on clicked element :) EDIT: oops looks like quite confusing sentence indeed: I'm looking for a way to add and remove class on the same button and regarding OP's posted code
@A.Wolff Actually I wanted to clarify it but he didnt respond so i assumed that the clicked button but then i changed my answer hoping this is what OP wants
0

You can use .add() method with .toggleClass():

$(document).ready(function() {
  $('button.toggle-portfolio').on('click', function(e) {
    $('.portfolio-contact-form-wrap').add(this).toggleClass('show');
  });
});
.portfolio-contact-form-wrap {
  color:blue;
}
.show {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="toggle-portfolio">Button</button>
<div class="portfolio-contact-form-wrap">
  <h1>contact form</h1>
</div>

Comments

0

Use toggleClass('show') It will add the class on one click and remove the class on the second click.

Comments

0
<script>
    $(document).ready(function () {
        $('button.toggle-portfolio').on('click', function (e) {
            $('.portfolio-contact-form-wrap').toggleClass('show');
        });
    });
</script>

1 Comment

for the nth time :)

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.