JavaScript How to acces variable from one function to another in javascript access variable from another class java,access variable from another

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 target='_blank' href="#x" class="overlay" onclick="var1(data)"></a>  
    <input type="button" value="Send" onclick="var2()"  />
 </body>
 </html>

Please anyone help me.

Answer:1

It's a question of scope. The way you have it in your code, a's scope is only within function var1. You need to define it outside the scope of the function for it to be available to other functions:

var a;
function var1(data) 
 {
   a = data;
 }
Answer:2

I think you should declare the variables as a global variable first, so you can access it anywhere.
Here it is:

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

       window.a = data;

     }

  function var2() 
     {

        window.b = window.a;
        alert(window.b);
     }
 </script>
 </head>
 <body>
 <a target='_blank' href="#x" class="overlay" onclick="var1(data)"></a>  
 <input type="button" value="Send" onclick="var2()"  />
 </body
 </html>

Try it! :D

Answer:3

try below code

var a;
function var1(data) 
 {

    a= data;

 }



function var2() 
 {

    var b = a;
    alert(b);
 }
Answer:4

This is because of the scope of the variables. This means that variables inside functions are not accessable to other functions that are on the same or a higher level. Functions can however use variables that have been made at a higher level.

If you want to access that variable, try returning it.

function var1(data) {
    return data
};

function var2() {
    var b = var1("This string is a"); // b = "This string is a"
    alert(b);
};

In this context, doing this makes no sense though.

Answer:5

First declare the variables before making a function:

jsfiddle

var a = "test";
function var1() {
    document.getElementById('data').value = a;
}

function var2()  {
    var b = a;
    alert(b);
}
Answer:6

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

  1. angularfire callback

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to node.js. for (var index in output) { if (opt.options....

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to node.js. for (var index in output) { if (opt.options....

  1. removing empty strings from a list in python
  2. removing empty strings from list
  3. remove empty strings from array javascript
  4. remove empty strings from list c#
  5. remove empty strings from array
  6. remove empty strings from array ruby
  7. remove empty strings from array php
  8. remove empty strings from array java
  9. remove empty strings from array js
  10. remove empty strings from list java
  11. remove empty strings from array c#
  12. remove empty strings from array python
  13. remove empty strings from list r
  14. remove empty strings from vector r
  15. remove empty strings from object javascript
  16. remove empty strings python
  17. remove empty strings r
  18. remove empty strings from list javascript
  19. remove empty strings
  20. remove empty strings from array swift

Does anyone know in which order the jQuery sortable events are being triggered? I'm asking this because I had a problem with stop and update. It made more sense to me that update event would come ...

Does anyone know in which order the jQuery sortable events are being triggered? I'm asking this because I had a problem with stop and update. It made more sense to me that update event would come ...

  1. jquery sortable order number
  2. jquery sortable order
  3. jquery sortable order save
  4. jquery sortable reorder list
  5. jquery sortable reorder array
  6. jquery sortable get order
  7. jquery sortable save order in database
  8. jquery sortable update order
  9. jquery sortable get order of elements
  10. jquery sortable set order
  11. jquery sortable save order in database mvc
  12. jquery sortable get order of items
  13. jquery sortable change order
  14. jquery ui sortable order
  15. jquery sortable change order programmatically
  16. jquery sortable save order mysql
  17. jquery sortable post order
  18. jquery sortable sort order
  19. jquery sortable event order
  20. jquery ui sortable order number

N.B. I'm aware that I add ids and combine these in a selector e.g. "#myDiv1,#myDiv2" so please refrain from suggesting this as it does not relate to my question. Is there a way to 'chain' the vars ...

N.B. I'm aware that I add ids and combine these in a selector e.g. "#myDiv1,#myDiv2" so please refrain from suggesting this as it does not relate to my question. Is there a way to 'chain' the vars ...

  1. jquery on click
  2. jquery on change
  3. jquery onload
  4. jquery on hover
  5. jquery on page load
  6. jquery on ready
  7. jquery on submit
  8. jquery on select change
  9. jquery onclick function
  10. jquery on scroll
  11. jquery on focus
  12. jquery on keyup
  13. jquery on multiple events
  14. jquery one
  15. jquery on button click
  16. jquery on resize
  17. jquery on checkbox change
  18. jquery on input
  19. jquery once
  20. jquery on input change