Pieter Schreurs February 2016

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.

Answers


Qwertiy February 2016

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.

Post Status

Asked in February 2016
Viewed 2,401 times
Voted 9
Answered 1 times

Search




Leave an answer