views:

31

answers:

3

Hi,

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?

+1  A: 
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.

sje397
+1  A: 

Do this:

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

The event argument is automatically passed.

Delan Azabani
+1  A: 
$('a').click();

or

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