Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to acces variable from one function to another in javascript

I want to access the value of variable a inside var2() function. However, it gives an error saying the value is not defined.

<html>
<head>
<script type="text/javascript">
  function var1(data) 
     {
        var a = data;
     }

  function var2() 
     {
        var b = a;
        alert(b);
     }
 </script>
 </head>
 <body>
    <a href="#x" class="overlay" onclick="var1(data)"></a>  
    <input type="button" value="Send" onclick="var2()"  />
 </body>
 </html>

Please anyone help me.

like image 574
user3048785 Avatar asked Jul 19 '26 01:07

user3048785


1 Answers

try below code

var a;
function var1(data) 
 {

    a= data;

 }



function var2() 
 {

    var b = a;
    alert(b);
 }
like image 50
Indranil.Bharambe Avatar answered Jul 21 '26 15:07

Indranil.Bharambe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!