Kaitlyn Sacco
Kaitlyn Sacco

Reputation: 43

on() to receive param like function instead of event?

the 1st param of on() might be click, mouseenter, etc event, but it's possible to bind function instead?

$(document).on('click','#box', function(e){
    $(this).css('padding-bottom', '32px');
});

I want not the click to apply on #box, but I can't set the 2nd param to be something else because I wan't to use $(this).. so I'm wondering whether 'click' can be some functions..

Upvotes: 0

Views: 48

Answers (2)

Drixson Oseña
Drixson Oseña

Reputation: 3641

Yes you can add/trigger (non-browser) events in on , see example:

$("p").on("myCustomEvent", function(event, myName){
  $(this).text(myName + ", hi there!");
  $("span")
    .stop()
    .css("opacity", 1)
    .text("myName=" + myName )
    .fadeIn(30)
    .fadeOut(1000);
});

$("button").click(function () {
  $("p").trigger("myCustomEvent", [ "John" ]);
});

Upvotes: 1

Ryan Miller
Ryan Miller

Reputation: 1063

I think you'll save yourself the headache by simply doing something like this:

$('#box').on('click', function() {
     $('document').css('padding-bottom', '32px');
}

Is there a particular reason you need to preserve the context of $(this) as referring to the document?

Upvotes: 1

Related Questions