DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Pass e parameter, jQuery

I have the following code in my document ready:

$('a').click(function(e) {
    e.preventDefault();
    var $this = $(this);
    var horizontalPadding = 30;
    var verticalPadding = 30;
    $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
        title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
        autoOpen: true,
        width: 800,
        height: 500,
        modal: true,
        resizable: true,
        autoResize: true,
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    }).width(800 - horizontalPadding).height(500 - verticalPadding);            
});

I want to do it this way:

$('a').click(aFunction())

but how do I pass the e parameter?

Upvotes: 1

Views: 1201

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

$('a').click();

or

$('a').trigger('click');

Upvotes: 1

Delan Azabani
Delan Azabani

Reputation: 81384

Do this:

function aFunction(e) { ... }
$('a').click(aFunction);

The event argument is automatically passed.

Upvotes: 1

sje397
sje397

Reputation: 41812

function aFunction(e) {
    e.preventDefault();
    var $this = $(this);
    var horizontalPadding = 30;
    var verticalPadding = 30;
    $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
        title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
        autoOpen: true,
        width: 800,
        height: 500,
        modal: true,
        resizable: true,
        autoResize: true,
        overlay: {
            opacity: 0.5,
            background: "black"
        }
    }).width(800 - horizontalPadding).height(500 - verticalPadding);            
}

$('a').click(aFunction);

jQuery will pass 'e' to the function you provide - but you must pass it without brackets, so you are passing a function object, and not the result of a function call.

Upvotes: 1

Related Questions