Skip to content Skip to sidebar Skip to footer

Jquery/javascript: Defining A Global Variable Within A Function?

I have this code: var one; $('#ma1').click(function() { var one = 1; }) $('body').click(function() { $('#status').html('This

Solution 1:

Remove the var from inside the function.

    $("#ma1").click(function() {
        one = 1;
    })

Solution 2:

If you want to make a global variable bind it to window object

window.one = 1;

Solution 3:

var one;//define outside closure

    $("#ma1").click(function() {
        one = 1; //removed var 
    })
    $("body").click(function(e) {
        $('#status').html("This is 'one': "+one);
    })

Post a Comment for "Jquery/javascript: Defining A Global Variable Within A Function?"