Using HTML5 Widgets

The left panel of the demonstration shows some of the many types of input available in HTML5. The right panel shows a button and different ways of using the select tag. We made use of the Google Style Guide, but (for ease of reading) kept most JavaScript event-handling code within the HTML5 tags rather than calling separate functions.


While we believe in the value of "learning by doing", in this area from our experience it is better described as "learning the hard way". We offer the following points of guidance:

  • It can be difficult to trace errors (which may be in your CSS, HTML5 tags or JavaScript). It is very quick to test that the appearance is as intended, so test frequently.
  • Remember to press F12 and look at the JavaScript console to see if it reports any errors. It may provide you with a line number in the source but you still may need to examine that line very closely to spot your error.
  • Composite names are linked with a hyphen in CSS (e.g. border-radius) but are camel case in JavaScript (e.g. borderRadius).
  • Values within HTML5 tags must be between double quotes (e.g. color = "green") but in CSS values in quotes are ignored and the syntax is color: green;. For learners with a Pascal background, strings look better in HTML tags and numbers look better in CSS!
  • In most cases white space is beneficial for readability but you must not put a space between a value and its units (e.g. type 10px not 10 px).
  • In a JavaScript if-then-else statement within HTML tags, put braces round even a single statement to be executed when the condition is true (e.g. our checkbox onclick event-handler in the code below the demo).
  • You are likely to appreciate all the more what Smart Pascal does for you after some coding from scratch in HTML5/CSS/JavaScript!


If this demo does not work in your current browser, try another such as Chrome. If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page.


The Code

<!DOCTYPE html>
/* both panels */
div {
  border-radius: 10px; 
  float: left;
  padding: 10px;   
  width: 180px;  

/* left panel */
#main {
  background-color: yellow;
  margin-right: 10px;

#btn1 {
  border: 1px solid black;
  border-radius: 4px;

#resize {
  width: 100px;

#txt1 {
  width: 150px;   

textarea {
  border: 2px solid #aaa;
  border-radius: 8px;
  height: 60px;
  margin-bottom: 10px;
  margin-top: 20px;
  overflow: hidden;
  resize: none;
  width: 170px;

/* right panel */ 
#right {
  background-color: green;

#s3 {
  overflow: hidden;

.bright {
  color: yellow;
  function getRadioVal(name) {
    // adapted from
    var val;
    // get list of radio buttons with specified name
    var radios = document.getElementsByName(name);
    // loop through list of radio buttons
    for (var i = 0; i < radios.length; i++) {
      if ( radios[i].checked ) { // radio checked?
        val = radios[i].value; // if so, hold its value in val
        break; // and break out of for loop
    return val; // return value of checked radio or undefined if none checked
  function multipleSelection() {
    var lb = document.getElementById('s3');
    var panel2 = document.getElementById('right');
    var title = document.getElementById('title2');
    if (lb.options[0].selected) = '0' 
    else = '10px';
    if (lb.options[1].selected) = '3px solid black'
    else = 'none';
    if (lb.options[2].selected) = 'hidden'
    else = 'visible';  
<div id = "main">
  <h4>Input Types Text, Button, Number, Range, Checkbox and Radio</h4>
  <p>Please enter your forename.</p>
  <input id = "txt1" type = "text" />
  <p id = "greeting">&nbsp;</p>
  <input type = "button" id = "btn1"  title = "input with type button" 
         onclick = "document.getElementById('greeting').innerHTML = 'Hello, ' +   document.getElementById('txt1').value + '!'"
         value = "Show greeting" />
  <p>Use spinner to change width of its box</p>  
  <input id = "resize" title = "Change the width" type = "number" min = "60" max = "180" step = "20" value = "100" 
         onchange = "if ((value >= 60) && (value <= 180)) document.getElementById('resize').style.width = value + 'px'" />
  <textarea readonly 
            id = "ta">Slider controls border radius of this read-only textarea. Checking the box enables vertical resizing.</textarea>
  <input type = "range" min = "0" max = "20" value = "8" 
         onclick = "document.getElementById('rng').value = value; document.getElementById('ta').style.borderRadius = value + 'px'" />
  <output id = "rng">8</output><br>
  <input type = "checkbox" 
         onclick = "if (checked) {getElementById('ta').style.resize = 'vertical'} 
                    else {getElementById('ta').style.resize = 'none'}">Resizeable textarea</input><br>
  <input type = "radio" name = "colours" value = "yellow" checked = "true" 
         onclick = "document.getElementById('main').style.backgroundColor = getRadioVal('colours')">Yellow panel</input><br>
  <input type = "radio" name = "colours" value = "green" 
         onclick = "document.getElementById('main').style.backgroundColor = getRadioVal('colours')">Green panel</input><br>
  <input type = "radio" name = "colours" value = "blue" 
         onclick = "document.getElementById('main').style.backgroundColor = getRadioVal('colours')">Blue panel</input>

<div id = "right">
  <h4 class = "bright" id = "title2">Button, ComboBox and ListBoxes (with Tooltips)</h4>  
  <p class = "bright">Click button to see JavaScript alert</p>  
  <button id = "btn2" color = "green" title = "Button with type button" 
          onclick = "alert('This is a button not an input with type button.')">More Info</button><br><br>   
  <p class = "bright">Change panel colour</p>  
  <select title = "Colour of form" 
          onchange = "document.getElementById('right').style.backgroundColor = value">
    <option value = "green">Green</option>
    <option value = "red">Red</option>
    <option value = "orange">Orange</option>  
  <p class = "bright">Change button text colour</p>  
  <select id = "s2" size = "3" title= "Colour of button text" 
          onchange = "document.getElementById('btn2').style.color = value">
    <option value = "green">Green</option>
    <option value = "red">Red</option>
    <option value = "orange">Orange</option>  
  <p class = "bright">Multiple selections with ctrl+click</p>
  <select multiple id = "s3" size = "3" title = "Miscellaneous" onchange = "multipleSelection()">
    <option>Traditional corners</option>
    <option>Black border</option>
    <option>Invisible title</option>  

Displaying Widgets using JavaScript

Append the following JavaScript code to the HTML file above and delete all of the HTML code for the div with id = "main", including the opening and closing div tags. (The process of copying, deleting and appending worked for us using Notepad++ but with Notepad the JavaScript-coded panel failed to appear). The JavaScript code provides the same functionality for the left panel, at least when viewed in the Chrome browser. See also our lightweight Smart Pascal application for displaying the same panel.

  var h4 = document.createElement("h4");
  h4.innerHTML = "Input Types Text, Button, Number, Range, Checkbox and Radio";
  var para1 = document.createElement("p");
  para1.innerHTML = "Please enter your forename.";

  var para2 = document.createElement("p");
  para2.innerHTML = "&nbsp;"; = "greeting";

  var para3 = document.createElement("p");
  para3.innerHTML = "Use spinner to change width of its box";

  var input = document.createElement("input"); = "txt1";

  var btn = document.createElement("input");
  btn.type = "button"; = "btn1";
  btn.title = "input with type button";
  btn.setAttribute("onclick", "document.getElementById('greeting').innerHTML = 'Hello, '" +
                   "document.getElementById('txt1').value + '!'");
  btn.setAttribute("value", "Show greeting"); 

  var spinner = document.createElement("input");
  spinner.type = "number"; = "resize";
  spinner.title = "Change the width";
  spinner.value = 100;
  spinner.min = 60;
  spinner.max = 180;
  spinner.step = 20;
  spinner.setAttribute("onchange", "if ((value >= 60) && (value <= 180))" +
                       "document.getElementById('resize').style.width = value + 'px'");

  var text_area = document.createElement("textarea");
  text_area.innerHTML = "Slider controls border radius of this read-only textarea." + 
                        "Checking the box enables vertical resizing." = "ta";
  text_area.setAttribute("readonly", "true");
  var range = document.createElement("input");
  range.setAttribute("type", "range");
  range.setAttribute("min", "0");
  range.setAttribute("max", "20");
  range.setAttribute("value", "8");
  range.setAttribute("onclick", "document.getElementById('rng').value = value;" + 
                                "document.getElementById('ta').style.borderRadius = value + 'px'");

  var output = document.createElement("output"); = "rng";
  output.value = 8;

  var checkbox = document.createElement("input");
  checkbox.type = "checkbox"; = "cb";
  var lbl1 = document.createElement("label");
  lbl1.innerHTML = "Resizeable textarea";
  checkbox.setAttribute("onclick", "if (checked) {getElementById('ta').style.resize = 'vertical'}" +
                        "else {getElementById('ta').style.resize = 'none'}");
  // Radio buttons       
  var rb1 = document.createElement("input");
  rb1.type = "radio"; = "rbtn1"; = "colours"; 
  rb1.value = "yellow";  
  rb1.checked = true;
  rb1.setAttribute("onclick", "document.getElementById('main').style.backgroundColor = 'yellow'");
  var lbl2 = document.createElement("label");
  lbl2.innerHTML = "Yellow panel";

  var rb2 = document.createElement("input");
  rb2.type = "radio"; = "rbtn2"; = "colours"; 
  rb2.value = "green"; 
  rb2.checked = false;
  rb2.setAttribute("onclick", "document.getElementById('main').style.backgroundColor = 'green'");
  var lbl3 = document.createElement("label");
  lbl3.innerHTML = "Green panel";

  var rb3 = document.createElement("input");
  rb3.type = "radio"; = "rbtn3"; = "colours"; 
  rb3.value = "blue"; 
  rb3.checked = false;
  rb3.setAttribute("onclick", "document.getElementById('main').style.backgroundColor = 'blue'");
  var lbl4 = document.createElement("label");
  lbl4.innerHTML = "Blue panel";
  // Create and populate the left panel
  var left_panel = document.createElement("div"); = "main";

  var br1 = document.createElement("br");

  var br2 = document.createElement("br");

  var br3 = document.createElement("br");

  var br4 = document.createElement("br");

  var right = document.getElementById("right");
  document.body.insertBefore(left_panel, right);

Programming - a skill for life!

How to apply your knowledge of Pascal when learning JavaScript