Kheera
Kheera

Reputation: 21

Switch case not working - cant find value

Here is my question:

I'm trying to get a question form in html5 / javascript with using 2 switch cases to show different questions and answers on the field. I'm using var box for the questions and var answers for the answers (with innerHTML).

Anyways, when I am in init() for the first time, it should check for answers.value from the innerHTML post, it can't find the answers.value when i use it, I would love some help on this.

<script type="text/javascript">
            window.addEventListener('load', function() {

                    var box = document.getElementById('vragen');
                    var answers = document.getElementById('antwoorden');

                    box.innerHTML = 'Kun je je print pas met contant geld opwaarderen?';
                    answers.innerHTML = '<input type="button" value="Antwoord 1" onclick="init()"><br><input type="button" value="Antwoord 2" onclick="init()"><br><input type="button" value="Antwoord 3" onclick="init()"><br><input type="button" value="Antwoord 4" onclick="init()">'; //onclick function

                    var foutenCounter;
                    var antwoord = '';
            });

            function init() {

            switch(answers.value) {
                case 'Antwoord 1': 
                alert("test");
                antwoord = 'a';
                    switch(antwoord) {
                        case 'a':
                            alert('goed');
                        break;

                        case 'b':
                            //fout example.
                            alert('fout');
                        break;

                        case 'c':

                        break;

                        case 'd':

                        break;
                    }
                break;

Upvotes: 0

Views: 98

Answers (1)

rlf
rlf

Reputation: 59

answers wont be defined in your init function. You should define var answers globally then you should be able to both set it on load and use it in init.

Upvotes: 1

Related Questions