#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Get JS variable from external JS


    Hi,

    I need to get a variable value from dropdown and pass this to another JS file:


    On <SELECT> i have Onchange event, which get dropdown value.

    If I make selection, alert popup appear with correct value

    file.php


    Code:
    ... script ...
    function __doPostBack(elm) {
    var user_id = elm.options[elm.selectedIndex].value;
    alert(user_id);
    }
    ... script ...
    My trouble is, how to get user_id value on another JS page?

    I'm triyng to get dropdown_value variable on external commonn.js but is blank

    Code:
    var dropdown_value = user_id;

    Can anyone help me?

    Thanks

    Cris
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,298
    Rep Power
    7170
    Are you trying to use the variable on a different page request, or just from a different JavaScript file that is part of the same page request as the first JavaScript file?

    If the latter, then you have at least a scope problem. Since you declare user_id as a var inside __doPostBack, it will only exist within __doPostBack. If you want it to be accessible globally, then you need to declare it as a var in the global scope.

    You may also have a timing problem; ie: you need to make sure the second code isn't being executed before you assign a value to user_id in the first code.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo