hashtagbad
hashtagbad

Reputation: 107

Passing jquery variable to another function

Not sure why this isn't working, and it's returning as an object/undefined.

I have a variable outside above the doc.ready:

var video_box_original_link = '';

I have a link that I click on inside the ready function which grabs the info fine:

$(document).on('click', '.youtube_video', function(video_box_original_link){
    var my_game_name = $('a.game_title_link').attr('name');
    var my_game_platform = $('a.game_platform_link').attr('name');
    video_box_original_link = "/"+my_game_platform+"/"+my_game_name+"/videos";
});

And I have a function that I'm trying to pass to this which is also in the doc.ready down below:

$(document).on('click', '.popblock_box', function(e, video_box_original_link){
    window.history.pushState("vidPage", "vidPopped", ""+ video_box_original_link +"");
}

Is this should be fine?

The "e" is for something else in the script which is not necessary to show.

Upvotes: 1

Views: 39

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You couldn't pass variable to callback like you do like you do in function(video_box_original_link) you should define it in global scope then just use it :

var video_box_original_link = '';

$(document).on('click', '.youtube_video', function(){
    var my_game_name = $('a.game_title_link').attr('name');
    var my_game_platform = $('a.game_platform_link').attr('name');

    video_box_original_link = "/"+my_game_platform+"/"+my_game_name+"/videos";
});

$(document).on('click', '.popblock_box', function(e){
    window.history.pushState("vidPage", "vidPopped", ""+ video_box_original_link +"");
}

Hope this helps.

Upvotes: 1

Related Questions