Joshua
Joshua

Reputation: 1126

jQuery parse object to a secondary function

I am trying to parse $(this) to a function called by another function but without much luck. Essentially my question is; is it possible and if so, how?

Code is as follows:

Initial Call

$('.playform').on('click', playForm);

Primary Function

function playForm(e){
    ...snip...
TweenLite.to($('.video'), 1, {css:{'height':win.h+'px'}, ease:Power4.easeInOut, onComplete:showForm});
    ...snip...
}

Secondary Function

function showForm{
   $('.video #youtube').append('<iframe class="show-form" src="'+$('.playform').attr('href')+'"></iframe>');
}

As I am going to be using more than one form, I wanted to automate this process but I cant seem to

Things I have tried so far but to no avail

  1. Declared $(this) as a variable
  2. Tried to give the functions parameters

Upvotes: 1

Views: 85

Answers (2)

VIDesignz
VIDesignz

Reputation: 4783

Step 1

$('.playform').on('click', function(){
    var whichVid = $(this);
    playForm(whichVid);
});

Step 2

function playForm(e){
    TweenLite.to($('.video'), 1, {css:{'height':win.h+'px'}, ease:Power4.easeInOut, onComplete:showForm(e)});

}

Step 3

function showForm(e){
 $('.video #youtube').append('<iframe class="show-form" src="'+e.attr('href')+'"></iframe>');
}

Alternatively you could establish a global variable before step 1 and then set the value on the click event like

var window.whichVid = '';

$('.playform').on('click', function(){
    window.whichVid = $(this);
    playForm();
});

function playForm(){
    TweenLite.to($('.video'), 1, {css:{'height':win.h+'px'}, ease:Power4.easeInOut, onComplete:showForm()});

}

function showForm(){
 var thisVid = whichVid.attr('href');
 $('.video #youtube').append('<iframe class="show-form" src="'+ thisVid +'"></iframe>');
}

Upvotes: 1

whale_steward
whale_steward

Reputation: 2258

you could put it like this (not tested) :

(function($){
    var global_this;

    function palyform(e){
        global_this = $(this)
        ...
    }

    function showform(e){
        // do shomething with global_this
        ...
    }

    $('.playform').on('click', playForm);
}(jQuery);

Upvotes: 0

Related Questions