jeudi 2 mars 2017

JS onclick only works the first time

I am new to JS so excuse my ignorance. I am creating a button and I want the button to toggle what it says. I can get it to fire once, but then it won't fire again unless i refresh the page. What am I doing wrong? I'm sure its simple.

JS function:

<script type="text/javascript">
        function fullscreen(){
            var elem = document.getElementById("button1");
            if (elem.value=="Maximize"){
                elem.value = "Minimize";
            }else {
                elem.value = "Maximize";}
        }
</script>

in the body of the page is:

<div>    
    <input type="button" id="button1" value="Minimize" onclick='fullscreen()'>
</div>

What I expect is the text of the button will switch when the variable is passed through the if statement. I can get it to change from "minimize" to "maximize" onclick, but the second click does nothing.

Aucun commentaire:

Enregistrer un commentaire