Keep variable global outside .submit jquery function

Discussion in 'jQuery' started by Radulescu Iulia, Nov 27, 2013.

  1. #1
    Hello all,

    I want to keep a variable global, and access its content outside the .submit handler function below:

    $(document).ready(function() {

    name="1";
    pass = "2";

    /*helper function that changes name and pass values:
    name = the value contained in the name input text field of the form
    pass = the value contained in the pass input text field of the form*/

    $("#loginForm").submit(function() {

    /*changes values correctly*/
    }

    alert(name);
    Alert gives me value "1" instead of the value attributed in the function.

    How can I transfer the value inside the function to an outside variable?

    Thanks in advance,
    Iulia
     
    Radulescu Iulia, Nov 27, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    If a variable is set in the global scope ie:

    [script]
    <script>
    var a = 'a';

    function test() {
    alert(a);
    }

    test();
    </script>
    [/script]

    As you can see "a" is accessible to the test() function even thou we haven't passed it to it. Can you post some of your code?
     
    HuggyStudios, Nov 28, 2013 IP