0

I want click a button then it add the class

.overflow{overflow-y:scroll};

I used addClass('overflow'}, but it reload the whole page on clicked.

After an action it will removeClass('overflow') I will not choose to use .css('overflow','hidden') because 'auto','scroll','hidden' is not suitable for me, I want it being completely remove after used.

1
  • Please show the relevant code. Maybe in a fiddle. It's not clear why your button would reload the page. Commented Nov 29, 2012 at 14:43

4 Answers 4

2

Why dont you just use an <a> with href="#" ?

That wouldn't reload the page and still trigger your script.

In yor posted code u have a minor typo: You ended the addClass() with } ... This would be the correct code:

$("#targetElement").addClass('overflow');

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

1 Comment

It's better to use href="javascript:void(0);", otherwise a click would scroll back to the top
2

To prevent the page to be reloaded:

$("#yourbuttonid").click(function(e){
   e.preventDefault();  // this will prevent the link to be followed
   //the rest of your code
});

Comments

1

In order to prevent page reloading, you should prevent default anchor click event:

$("a.button").on("click", function(e) {
    // ... addClass("overflow");

    e.preventDefault();  // or instead you may use
                         // return false;
});

Comments

0
$("#yourbuttonid").click(function(e){

    //your code

    e.preventDefault();  // this will prevent the link's default action
                         // make sure it comes last in your code,
                         // if not it will cancel your code from executing.

});

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.