To select a switch/case statement each click on the button

Asked

Viewed 607 times

1

I would like to make list(Gem) of A-Z.

A detail, based on breaking/dividing this list(Gem) alphabet into parts:

Although my difficulty is in, after being clicked on the button, the same bring up the list so:

1st click the button - then the first part be charged in this order.

To B C D And F G H I

2nd click the button - then it is loaded to second part.

J K L M N The P Q V

3rd click on the button - then will end the third part with the rest of the alphabet.

R S T U W X Y Z

Each time I click the same button, I want to add +1 to the variable.

For example: I have a variable count receiving the value 0 initial, by clicking this button the same begins the count++ to 1 and so on, executing each declaration block within the stwich/case.

For more details note the logic below:

<script>
 window.onload = function(){

 var contador = 0;
 contador++;

 document.getElementById("conta").onclick = function(){
 document.getElementById("resultado").innerHTML=contador;


switch (contador){
 case "1":
 for ( i = 1; i < 9; i++ ){
 ler = String.fromCharCode(i+64);
 document.body.innerHTML += ler +"<br>";
 }
 break;
 case "2":
 for ( i = 1; i < 9; i++ ){
 ler = String.fromCharCode(i+64);
 document.body.innerHTML += ler +"<br>";
 }
 break;
 case "3":
 for ( i = 1; i < 8; i++ ){
 ler = String.fromCharCode(i+64);
 document.body.innerHTML += ler +"<br>";
 }
 break;
 alert("Ok! Fim de Listagem.\nRetome sua pesquisa.");
}
</script>

However, I have faced difficulty in making this selection every click on the button.

Any hint, suggestion and simple and/or practical examples, everything to contribute, will be welcome as an answer.

1 answer

1


var c = 0
var result = document.getElementById('result');

function a() {
    result.innerHTML = '';
    switch (c.toString()) {
        case "1":
            for (i = 1; i < 9; i++) {
                ler = String.fromCharCode(i + 64);
                result.innerHTML += ler + "<br>";
            }
            break;
        case "2":
            for (i = 9; i < 17; i++) {
                ler = String.fromCharCode(i + 64);
                result.innerHTML += ler + "<br>";
            };
            break;
        case "3":
            for (i = 17; i < 27; i++) {
                ler = String.fromCharCode(i + 64);
                result.innerHTML += ler + "<br>";
            }
            break;
    }
    if (c == 3) {
        c = 0;
    }
    alert("Ok! Fim de Listagem.\nRetome sua pesquisa.");
}
<input type="button" onclick="a(c++);" value="click me">
<div id="result">
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.