Fash Footwear
Fash Footwear

Reputation: 39

javascript - how to get value of variable from other function

please check my below javascript functions. i want to call user in my second function showUser. i tried but not able to get value. its giving me undefined.

function one

<script>
    var user;
    function choose(choice){
        user = choice;
        alert("You got the value " + user);
    }
</script>

function second where i want to call var user from above function.

<script type="text/javascript">
    function showUser(str)
    {
        if (str=="")
        {
            document.getElementById("txtHint").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        } else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        var buttonValue = document.getElementsByName('buttonpassvalue').item(0).value;
        var buttonValue1 = user;
        alert("checking buttonvalue1  " + buttonValue1);
        var drdownValue = document.getElementsByName('shorting').item(0).value;

        xmlhttp.open("GET", "productlistajax.php?q=" + drdownValue + "&version=" + buttonValue + "&version1=" + buttonValue1, true);

        xmlhttp.send();
    }
</script>

Upvotes: 1

Views: 82

Answers (1)

Peter Bankuti
Peter Bankuti

Reputation: 174

there is something else wrong, this (which is basically what you want as i understand) should work:

var user;

function choose(choice)
{
  user = choice;
  alert("You got the value " + user);
}

function showUser(str)
{
  var buttonValue1 = user;
  alert("checking buttonvalue1  " + buttonValue1);
}

choose('test');
showUser('whatever');

Upvotes: 1

Related Questions