3

I am trying to access the javascript variable value from this code to ajax code

I tried to access like this but it is not working-

javascript code

<script type="text/javascript">

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
       var a = radios[i].value
            break;
        }
    }
}


function remove(a) 
{
     alert(a);
     if(a=="") 
     {
         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;
         }
     }
     xmlhttp.open("GET","theaterdel.php?q="+a,true);
     xmlhttp.send();
}

</script>

In this code I am trying to access the variable a's value from getResults() function to remove() like this

remove(a)
{}

But it is not working, when I alert a's value inside remove(a){}, it is printing undefined

5 Answers 5

4

Globally define the variable -

var a= "hi";

function getResults(){ ... }

function remove() { alert(a); ... }
Sign up to request clarification or add additional context in comments.

Comments

4

try this

function getResults() 
{
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) 
    {       
        if (radios[i].checked) 
        {
            var a = radios[i].value
            remove(a);
            break;
        }
    }
}

call a function inside

hope it will help.

Comments

3

You have declared variable "a" as local to getresults function. Declare variable "a" as global, don't use var keyword.

if (radios[i].checked) 
    {
     a = radios[i].value
        break;
    }

You are having two different functions and to get variable of one in another function, declare it globally.

Comments

2

you need to declare the variable outside the function like

var a = null
function getResults()

when you declare it outside the function you can access the variable from anywhere you want

also, look into jQuery .ajax() for simpler code regarding AJAX

Comments

2

You've defined a inside the getResults() function using the 'var' keyword. This means it will only be in scope inside that function.

If you want to use that variable in another function, you'll need to pass it across when you call the other function - simple example is below:

function myFunction() {
 var a = "this is a";
 secondFunction(a);
}

function secondFunction(a) {
 alert(a);
}

An alternative is to declare the variable so it has global scope - but that's not a great idea unless you're confident that it won't conflict with any other global variables that might exist on your page.

You're better off constraining scope unless you absolutely need to have global variables. It'll make your code neater, and debugging easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.