← HTML practicum

Lesson 10 · 17 steps

Buttons and controls

controls.html is where the site stops being something to read and starts being something to use: buttons, a text box, checkboxes, a grouped list, a slider, a colour picker. Also the only line of JavaScript in the whole practicum.

Hand in controls.html controls1.html sent.html
What the browser makes of it open on its own ↗
02A button that goes somewhereThe task: a button on controls.html that moves to another page.code ↓
<!-- 2) кнопка, яка веде на іншу сторінку -->
<h2> Кнопка </h2>
<input type="button" value="На другу сторінку" onClick='location.href="controls1.html"'>

A button on its own does nothing at all — it needs to be told what happens on a click. onClick holds a line of JavaScript, and location.href is the browser's address bar: assign to it and the page changes.

This is the first and only script in the whole practicum, and it is one line.

Today

Code written inside an attribute is awkward to maintain and is blocked by the security policies most sites set these days. The same button today is a link styled as a button, or a real <button> with its handler attached in a separate script file.

03A button made of a pictureThe task: a button with an image, using the im folder.code ↓
<!-- 3) кнопка-зображення -->
<h2> Кнопка-зображення &lt;input&gt; </h2>
<a href="controls1.html">
  <input type="image" src="im/button.png" alt="Кнопка з малюнком">
</a>

type="image" turns a picture into a submit button. The picture is im/button.png — drawn for this site rather than taken from somewhere.

05A link dressed as a buttonThe task: a link styled to look like a button.code ↓
<!-- 5) посилання, оформлене як кнопка -->
<style type="text/css">
.new1 {border:1px solid #f0a500;background:#3a2a1a;color:#f0a500;text-decoration:none;padding:6px 14px}
<!-- 5) посилання у вигляді кнопки -->
<h2> Посилання, схоже на кнопку </h2>
<a class="new1" href="controls1.html"> Кнопка </a>

A border, a background and text-decoration:none, and a link looks exactly like a button. The difference is underneath: a link navigates, a button acts, and the browser treats them differently for the keyboard and for opening in a new tab.

Today

Choosing between them is a real decision, not a matter of taste: if the result is «I am now on a different page», it is a link. If it is «something happened right here», it is a button.

04The same move, through a formThe task: buttons that move between controls.html and controls1.html using forms with the LINK and GET methods.code ↓
<!-- 4) кнопка переходу формою, метод GET -->
<h2> Перехід формою </h2>
<form method="GET" action="controls1.html">
  <input type="submit" value="Перейти формою">
</form>

A form with method="GET" and nothing to fill in is a button that leads to a page. The method matters later, when there is something to send.

Today

The LINK method in the assignment's sample is not a real HTTP method — no browser has ever implemented it. GET is used here instead, which does the same thing and actually works.

06A text boxThe task: a text field with the information sent by POST.code ↓
<!-- 6) текстове поле, метод POST -->
<h2> Текстове поле </h2>
<form method="GET" action="sent.html">
  <p> Розкажіть про свій найкращий раунд: </p>
  <textarea name="story" cols="50" rows="6"></textarea><br>
  <input type="submit" value=" Надіслати ">
</form>

textarea is a multi-line field; cols and rows set its size in characters and lines. The name is the label the answer travels under.

The form points at sent.html, because this site has no server to receive anything. Submitting it shows what the browser sent — visible in the address bar, since the method is GET.

Today

That is exactly the difference between the two methods: GET puts the answers in the address, POST sends them in the body of the request where they are not visible. Neither is secret — only HTTPS makes it private — but a password in the address bar would end up in the history, in logs and in bookmarks.

07CheckboxesThe task: a list of test questions with checkboxes; the questions must match the site's subject.code ↓
<!-- 7) прапорці -->
<h2> Прапорці </h2>
<form method="GET" action="sent.html">
  <input type="checkbox" name="dust2" checked> Ви грали на Dust II?<br>
  <input type="checkbox" name="mirage" checked> Ви грали на Mirage?<br>
  <input type="checkbox" name="nuke"> Ви грали на Nuke?<br>
  <input type="checkbox" name="ancient"> Ви грали на Ancient?<br>
  <input type="submit" value=" Надіслати ">
</form>

Each checkbox is independent, so they all need different names. checked ticks one from the start.

Today

Clicking the text should toggle the box, and here it does not. The fix is <label> around both, which also gives screen readers the question that belongs to the box.

09Radio buttonsThe task: a radio control allowing one choice out of several, plus a submit button.code ↓
<!-- 9) перемикачі -->
<h2> Перемикач </h2>
<form method="GET" action="sent.html">
  <p> Скільки років ви граєте? </p>
  <input type="radio" name="years" value="1"> менше року
  <input type="radio" name="years" value="2"> рік
  <input type="radio" name="years" value="3" checked> два-три
  <input type="radio" name="years" value="4"> більше п'яти
  <br>
  <input type="submit" value=" Надіслати ">
</form>

Radio buttons work the opposite way: the same name on all of them is what makes them one group, and picking one releases the others. A different name on each would give four independent switches.

10A list with groupsThe task: a scrolling list split into four groups.code ↓
<!-- 10) список із прокруткою, розбитий на групи -->
<h2> Список із групами </h2>
<select name="mylist" size="9">
  <optgroup label=" Автомати ">
    <option value="1"> AK-47 </option>
    <option value="2"> M4A4 </option>
    <option value="3"> M4A1-S </option>
  </optgroup>
  <optgroup label=" Снайперські ">
    <option value="4" selected="selected"> AWP </option>
    <option value="5"> SSG 08 </option>
  </optgroup>
  <optgroup label=" Пістолети ">
    <option value="6"> Desert Eagle </option>
    <option value="7"> Glock-18 </option>
  </optgroup>
  <optgroup label=" Гранати ">
    <option value="8"> димова </option>
    <option value="9"> світлошумова </option>
  </optgroup>
</select>

select is a list to choose from, size says how many rows are visible at once, and optgroup gathers options under a heading that cannot itself be chosen.

13The input types HTML5 broughtThe task: add a slider, a search box, a week, a date and time, and a colour.code ↓
<!-- 13) повзунок -->
<h2> Повзунок: скільки раундів до перемоги </h2>
<input type="range" min="3" max="13" step="1">
<!-- 14) пошук -->
<h2> Пошук </h2>
<label for="site-search"> Пошук: </label>
<input type="search" id="site-search" name="q" aria-label="Пошук по сайту">
<button> Знайти </button>
<!-- 15) тиждень -->
<h2> Тиждень турніру </h2>
<input type="week">

<!-- 16) дата і час -->
<h2> Дата й час матчу </h2>
<input type="datetime-local">
<!-- 17) колір -->
<h2> Колір прицілу </h2>
<form action="sent.html" method="GET">
  <p align="left"> Виберіть колір прицілу </p>
  <input type="color" name="color1">
  <input type="submit" value="Send">
</form>

These are all the same input tag with a different type: range, search, week, datetime-local, color. The browser supplies the slider, the calendar and the colour picker — a few years earlier each of those meant a library of JavaScript.

A phone goes further and shows a different keyboard for each type. It costs one word in the markup.