1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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