dotnetnoob
dotnetnoob

Reputation: 11340

How do I make this javascript reusable?

I have some code that I use many times. Here's the code and an example use:

$('.close-cookie-notice').click(function (e) {

    // make this reusable
    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        e.returnValue = false;
    }
    // end of reusable code
    $('#site-cookie-notice').slideUp();
});

The code I need to reuse is between the comments. I would like this to reside in its own function but am not sure how to deal with the passing of the event (e).

Anyone help?

Upvotes: 0

Views: 73

Answers (1)

PP.
PP.

Reputation: 10864

function myspecialfunction(e) {
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

Used as:

$('.close-cookie-notice').click(function (e) {
    myspecialfunction(e);
    $('#site-cookie-notice').slideUp();
});

Upvotes: 1

Related Questions