Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call the same jQuery function both on load and on click

In my "update" pages I have often the need to call the same jQuery function both on page load and on click of some checkbox, mostly like this:

function foo(){
    if ($('#mycheckbox').is(':checked')){
        $('.myclass').hide();
    } else {
        $('.myclass').show();
    }
}

$(function() {

    foo(); // this is launched on load
    $('#mycheckbox').click(function(){
        foo(); // this is launched on checkbox click
    })  

});

On page load, the checkbox could be checked or not depending on database values: that's why I have the need to launch the foo(); on load

Well, this works fine, but I always wondered if... there is a better or most elegant solution? Thanks in advance

like image 832
Ivan Avatar asked Nov 23 '11 15:11

Ivan


People also ask

Can we use two events together in jQuery?

To trigger the same function with multiple events, use the jQuery on() method with multiple events such as click, dblclick, mouse enter, mouse leave, hover, etc.

How to onclick event in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.

How to bind jQuery?

jQuery | bind() with ExamplesThe bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs. event: This is an event type which is passed to the selected elements.


3 Answers

you could avoid the second function call and bind the function foo directly to the click handler:

function foo(){
    if ($('#mycheckbox').is(':checked')){
        $('.myclass').hide();
    } else {
        $('.myclass').show();
    }
}

$(function() {
    foo(); // this is launched on load
    $('#mycheckbox').click(foo)  

});
like image 102
Nicola Peluchetti Avatar answered Sep 28 '22 04:09

Nicola Peluchetti


You could use the trigger() function to trigger the event immediately;

$('#mycheckbox').click(function (){
    if ($('#mycheckbox').is(':checked')){
        $('.myclass').hide();
    } else {
        $('.myclass').show();
    }
}).trigger('click'); 

However, bear in mind that this will also trigger any other click handlers you have defined on the element.

Alternately, you could write a tiny jQuery plugin to do it for you;

jQuery.fn.nowAndOn = function (event, handler) {
    handler();

    this.bind(event, handler);

    return this; // support chaining
};

And you'd use it like;

$(function() {
    $('#mycheckbox').nowAndOn('click', function (){
        if ($('#mycheckbox').is(':checked')){
            $('.myclass').hide();
        } else {
            $('.myclass').show();
        }
    });
});

However, the problem with this is that inside the click handler, this is normally the current element; whereas by calling handler(); like we do here, this would point to window instead (this isn't relevant in the exact snippet you posted, as you don't use this).

To fix this, we could add some complexity and use the Function.call method to set the value of this;

jQuery.fn.nowAndOn = function (event, handler) {
    this.each(function () {
        handler.call(this);
    });

    this.bind(event, handler);

    return this;
};

However, you'll still have a problem if you rely on an Event object being passed...

like image 26
Matt Avatar answered Sep 28 '22 06:09

Matt


One way to do it (assuming that your site contains a server side code component) would be to hide/show the item in the server side render, as appropriate. This means that there is no screen 'flicker' (however slight it is) on load of the page.

like image 25
Paddy Avatar answered Sep 28 '22 06:09

Paddy