FatDogMark
FatDogMark

Reputation: 1205

how to addClass('overflow');

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.

Upvotes: 0

Views: 126

Answers (4)

Design Pesendorfer
Design Pesendorfer

Reputation: 134

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');

Upvotes: 2

Daniel Barde
Daniel Barde

Reputation: 2703

$("#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.

});

Upvotes: 0

VisioN
VisioN

Reputation: 145398

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;
});

Upvotes: 1

Gregoire
Gregoire

Reputation: 24832

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
});

Upvotes: 2

Related Questions