Pieter Schreurs
Pieter Schreurs

Reputation: 51

Javascript global resetting

I am somewhat new to Javascript have read some into it and got the following problem: I created a global var, i'm setting it in a function and i'm trying to call it in another function but it comes out as undefined. I've googled around and it seems it's called Javascript Hoisting Decleration. But i can't find the solution on this. This is my code.

<script type="text/javascript">
var value;
function getComboBox() {
    var value = document.getElementById('vereniging').value;
    console.log(value);
}
function isChanged() {
    console.log(value);
    var chboxs = document.getElementById('checkbox');
    var show = document.getElementById('hidden');
    if(chboxs.checked && value=='BNI') {
    show2.style.display= 'block';
    show.style.display='none';
    }
    if(chboxs.checked && value=='WTC') {    
    show2.style.display='none';
    show.style.display='block';
    }
} </script>

it sets correctly with the first function.

Upvotes: 2

Views: 39

Answers (1)

Qwertiy
Qwertiy

Reputation: 21380

var value = document.getElementById('vereniging').value;

You are declaring a new variable instead of writing into global one.

If you want to use global, remove var.

Upvotes: 1

Related Questions