dimanche 24 septembre 2017

If.. Then in javascript

I want to change the font size of a text depending on the current font size.

Algorithm:
On clicking the text,
If font size = 35, change it to 12
else if font size = 12, change it to 35


    <!DOCTYPE html>
    <html>
    <body>
    <h2>What Can JavaScript Do?</h2>

    <p id="demo" onclick="myFunction()">Click me.</p>

    <script>
    function myFunction() {
      if (document.getElementById('demo').style.fontSize='35px') {
        document.getElementById('demo').style.fontSize='12px'
      } else if(document.getElementById('demo').style.fontSize='12px') {
        document.getElementById('demo').style.fontSize='35x'
      }

    }
    
    </script>
    </body>
    </html>

It changed it 12, but never changes back to 35. What did I miss here?

Aucun commentaire:

Enregistrer un commentaire