I can't change permanently the background color of a simple html page using javascript

when I click in the button the backgrounf color of the change but after one seconde it disappear

this is the code

<!DOCTYPE html>
<html>
    <head>
        <title>ex7</title>
        <meta charset="utf-8"> 
        <script>
            function changerCouleur() {
                document.bgColor= "#FFFggF";
            }
        </script>
    </head>
    <body>
      <form>
          couleur  <input type="text" > <br>
          <button onclick="changerCouleur()">changer couleur </button>
      </form>
    </body>
</html>

1 thought on “I can't change permanently the background color of a simple html page using javascript”

  1. #FFFggF is not a valid color code.

    Please try this code:

    function changerCouleur() {
                    document.body.style.background = "#ff9933";
                }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!DOCTYPE html>
    <html>
        <head>
            <title>ex7</title>
            <meta charset="utf-8"> 
        </head>
        <body>
          <form onSubmit="return false">
              couleur  <input type="text" > <br>
              <button onclick="changerCouleur();">changer couleur </button>
          </form>
        </body>
    </html>

    I added the attribute onSubmit="return false" to the form because otherwise every time you click on the button the page is reloaded

    Reply

Leave a Comment