1

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
2
  • 2
    What do you mean by "parse" ? Commented Feb 12, 2014 at 2:05
  • He probably means "to pass a variable to another function" Commented Feb 12, 2014 at 2:21

2 Answers 2

1

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>');
}
Sign up to request clarification or add additional context in comments.

3 Comments

@TimVermaelen That's what I thought but I ignored my instincts x)
deleted my comment since you changed it, just mentioning ... :) also .prop('href') is more correct ^^ and another thing, since e is usually used as an event it becomes misleading since you are using e for this
thanks heaps. the first option was the one that I got to work. @VIDesignz
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.