Home>

How should I write the source code to change the color of the h1 text to the color selected in the pull-down menu? ??





JavaScript



Input



  • Answer # 1

    Then I will answer.

    Answer example

    <! doctype html>
    <html>
    <head>
    <meta charset = "utf-8">
    <title>JavaScript</title>
    <script>
    function changeColor () {
      document.getElementById ("titleText"). style.color = document.getElementById ("textcolor"). value;
    }
    </script>
    </head>
    <body>
     Input 
    <select id = "text color">
    <option value = "red">RED</option>
    <option value = "yellow">YELLOW</option>
    <option value = "pink">PINK</option>
    <option value = "green">GREEN</option>
    <option value = "black">BLACK</option>
    <option value = "purple">PURPLE</option>
    </select>
    <button onclick = "changeColor ()">OK</button>
    </body>
    </html>

    I think this will work.

    Point 1
    Change color in the header
    onclick is changeColor
    Pay attention to the acronym for color

    Point 2
    document.getElementById ("textcolor"). value
    You can get the value in the select tag with.

    How is it?