jeudi 18 février 2016

JS: click on different buttons should get printed different numbers on the screen, but it would print just one number

I 'm in the process of creating a calculator where I have a button for each number. When a button is pressed, depending on the id, the number gets printed on the screen. Each button adds the number pressed to that area of the screen.

This is an example of two buttons:

<button class="btn btn-default" id="1" onclick="APP.agregarNumeros()">1</button>
<button class="btn btn-default" id="2" onclick="APP.agregarNumeros()">2</button>
<p id="resultado"></p>

<script>    
var counterAPP = {
        agregarNumeros:function(){
            var resultado = "";
            if ($("#1")) {
                resultado += $("#resultado").text("1");
            }
            else if ($("#2")) {
                resultado += $("#resultado").text("2");
            }
        }
    };
    window.APP = counterAPP;

Now, if I click on any of the two buttons, I get "1" printed on the screen. If I click the button (any of them) again, nothing happens.

Why is that? Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire