Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Spring 2019

## Allow User Input

- input text field:
  `<input type="text" id="background_color" />`
- Get the value: `.value`

Example 8.5 - Javascript Events Illustrated - Example 8.5 (Without Styles) | <p>Background color: <input type="text" id="background_color"/><br/><button onclick="setBackgroundColor('simple4');" type="submit">Set background color</button></p><p id="simple4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lobortis orci a odio. Donec cursus, felis eu pulvinar ornare, sem turpis dapibus elit, eget laoreet eros quam vel dolor. Ut ante mauris, ullamcorper quis, posuere quis, lacinia a, felis. In tincidunt malesuada pede. Donec varius lacus sed nisi. Donec rhoncus est ac erat. Duis lobortis fringilla eros. Donec volutpat ligula eu neque. In hac habitasse platea dictumst. Donec in massa. Aenean lectus ante, gravida et, luctus id, posuere at, pede. Nam risus orci, rhoncus eu, aliquam ut, interdum ut, orci. Suspendisse rutrum, turpis sed molestie gravida, libero risus iaculis ligula, quis tempor ligula enim vel sem. </p> body {font-family: Calibri,Tahoma,Verdana,Helvetica,sans-serif;} #simple4 { padding: 1em; border: medium solid black; width: 75%; } function setBackgroundColor(myid) { mynode = document.getElementById(myid); mycolor = document.getElementById('background_color').value; mynode.style.backgroundColor = mycolor; }

- Markup
- Style
- Script

    <p>Background color:
    <input type="text" id="background_color"/><br/><button onclick="setBackgroundColor('simple4');" type="submit">Set background color</button></p>
    <p id="simple4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc lobortis orci a odio. Donec cursus, felis eu pulvinar ornare, sem turpis dapibus elit, eget laoreet eros quam vel dolor. Ut ante mauris, ullamcorper quis, posuere quis, lacinia a, felis. In tincidunt malesuada pede. Donec varius lacus sed nisi. Donec rhoncus est ac erat. Duis lobortis fringilla eros. Donec volutpat ligula eu neque. In hac habitasse platea dictumst. Donec in massa. Aenean lectus ante, gravida et, luctus id, posuere at, pede. Nam risus orci, rhoncus eu, aliquam ut, interdum ut, orci. Suspendisse rutrum, turpis sed molestie gravida, libero risus iaculis ligula, quis tempor ligula enim vel sem.</p>

In `style` element (`<style type="text/css">`) within `head` element:

    body {font-family: Calibri,Tahoma,Verdana,Helvetica,sans-serif;}
    #simple4 {
      padding: 1em;
      border: medium solid black;
      width: 75%;
    }

In `script` element within `head` element (`<script type="text/javascript">`):

    function setBackgroundColor(myid) {
        mynode = document.getElementById(myid);
        mycolor = document.getElementById('background_color').value;
        mynode.style.backgroundColor = mycolor;
    }

Copyright © David Heitmeyer