How do I use my jQuery extension in the on function?

Asked

Viewed 66 times

2

In jQuery, I know it is possible to extend and create functions for certain types of operations through $.fn.

Thus:

(function ( $ ) {

    $.fn.clickOut = function (callback) {

        var that = this;

        $(document).click(function (e) {

            if ( $(e.target).closest(that).size() > 0) {

                return;
            }

            $.isFunction(callback) && $.proxy(callback, that)(e);
        });
    }

})( jQuery )

The function works correctly. But how can I make it work with the use of on, because I need to assign it to dynamically created elements.

Is there any way to "create a function" (I don’t know how else to say this) for the on of jQuery?

Roughly speaking, I wanted it to work like this:

$('div').clickOut(); // Forma normal

// Forma que tentei, e não deu certo.
$('#my-uls').on('clickOut', 'li', function (...){}); 
  • Regarding the on we have this reference: http://answall.com/questions/5196/qual-a-diferen%C3%A7a-entre-o-onclick-Function-e-o-clickfunction

  • So you tried? $('#my-uls').on('clickOut', 'li', function (){ $(this).clickOut(); });

  • I’ll try again. It hadn’t worked out

3 answers

2


In fact the answer of Emir Marques hit the beam. The way is yes the trigger, which is necessary to trigger the event. Any event fired with trigger can be captured with on. Thus:

(function ( $ ) {

    $(document).click(function (e) {
        if ($(e.target).closest(that).size() > 0) {
            $(e.target).trigger('clickOut');
            return;
        }
    });

    // Feito isso, você pode usar:
    $('#my-uls').on('clickOut', 'li', function (){
        // faz algo
    }); 

})( jQuery )
  • Look, that stretch function ( $ ) { is to define the plugin. It wouldn’t be $(function(){ ?

  • With the $ becomes a document.ready. Without it, it’s just a closure to isolate scope and ensure that $ == jQuery. Depends on your need

  • From what I understood the need was to implement a method in jquery (in the clickOut case) that could be used in any element. So in the example I mentioned, he implements this method and then makes use of Rigger to simulate "on". In this example you would need one more step than to call Trigger and then implement on. That would be just right?

  • I understood that he wanted to use the on, @Emirmarques. Maybe the question is not clear at all.

0

For this you can use the Trigger method.

In this case instead of using:

 $(<seletor>).on("clickOut", function(){
     /* Faz algo */
 });

You use:

 $(<seletor>).trigger("clickOut", function(){
    /* Faz algo */
 });

Full example:

(function ( $ ) {

    $.fn.clickOut = function (callback) {

        var that = this;

        $(document).click(function (e) {

            if ( $(e.target).closest(that).size() > 0) {

                return;
            }

            $.isFunction(callback) && $.proxy(callback, that)(e);
        });
    }

    $(<seletor>).trigger("clickOut", function(){
        /* Faz algo */
    });

})( jQuery )
  • Put the selector together with the library code statement? I don’t think it’s a good idea. I’ll test to see if Trigger works

  • You do not put together the statement. I put inside the onload of the page. You use the same way as the "on", but exchange this for the "Trigger". See the example again, I edited the same to be clearer

0

From what I understand the question, you want to create a callback so that after executing the .on it performs its function clickOut.

For this you must call it within the function.

(function($){
    $.fn.alertX = function(x){
        alert(x)
    }
})(jQuery);

jQuery('body').on('click', 'input', function(){
    var id = jQuery(this).attr('id');
    jQuery(this).alertX(id);
});

Example

OBS: I’m still seeing if there is a way to always do this in the method, type prototype.

  • The only problem with all the answers is the following: I created clickout to define that when it is clicked out, an event will occur (which is the callback of the function). If I use the click, I will be assigning an event to click on the element. Then I think the clickout would be impaired.

  • @Wallacemaxters Do you want to create a clickOut that does not depend on the click? Then I think it is not possible, because the click away is first of all a click. In my reply, I tried to show how to make the on capture a non-standard event (custom).

  • I get it. I get it too! Think I leave the question or delete?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.