Spelchan.com Logo

From Scratch Web Games: A Beginners Guide to Game Development using HTML, CSS, and JavaScript

Chapter 5.9: Special Purpose Input

While we have spent a lot of time talking about the input element, there are still a few more types that can be specified that we should cover. These include reset, button, image (also a button), color, and file.

While there is already a button tag, there are input versions for specifying submit, reset, and buttons. While having both the ability to create buttons with an input element and a button element is redundant, this was most likely due for compatibility issues and for a desire to have the input element represent all types of input even if some of the types are duplicates of other elements.

For buttons we have already seen the submit input type which certainly makes sense as submitting a form clearly is a vital part of the form so having a separate input specifically for doing submissions makes a lot of sense. The reset type is the equivalent of a button with the type of button set to “reset.” The reset activity will set all the form elements back to their default states effectively clearing the form.

You can also have buttons in a form by using the input type button. This creates a button just like the button tag would, with the text to display in the button being in the value parameter. Just like with buttons, to do anything with them you need to write JavaScript to handle the buttons. They do not serve any other purpose with the form. Here is an example:

<form action="">
<fieldset>
<legend>input reset and buttons</legend>
<label>
Input some text then hit reset:
<input type="text">
</label><br><br>
<input type="reset"><br><br>
<input type="button" value="click me" onclick="buttonDemo()">
<span id="inputButtonClicked"></span>
</fieldset> </form>

And the function in the script element:

function buttonDemo() {
document.getElementById("inputButtonClicked").
innerHTML = "Thanks for clicking me!";
}
input reset and buttons



The image input type is kind of misleading as it is a button. More specifically, it is a submit button. The idea is that you may want to use an image for your submit button so you would use image instead of submit and provide an image. The image type works the same way as the img tag does for specifying the image to use. You have to have an alt attribute to describe the image, src to specify the image file to use, width and height to specify the dimensions of the image with scaling happing if the size specified does not match the image size.

In this empty form we are just going to the same page but doing so with a fancy looking button.

<form action="">
<fieldset>
<legend>Image type used as submit button</legend>
<input type="image" src="imageTagSubmit.png"
width="512" height="32"
alt="fancy submit button">
</fieldset>
</form>
Image type used as submit button

Colors can also be selected. This usually is to allow users to customize some aspect of their browser but can be used for any time you need a color selected. Once we learn about the canvas tag in a later chapter, we could easily use the color tag to create a simple paint program. The value holds a html hex color code and the selected colour will be converted to the color code. You can then use the value to adjust colors. Here is a simple example of the color tag:

<label>
<input id="colorPicker" type="color" value="#660077"
onchange="changeColor()"> Pick your favorite color.
</label>
<p id="colorText" style="color:#607;">Here is text in the selected color.</p>

Here is text in the selected color.

Finally, there is the file input type. This brings up a dialog box to allow the user to select a file that they want the web page to work with. This use to involve sending the file to the server, something that can still be done in forms, but with the introduction of Web Workers we also got the FileReader API which we will cover in a later chapter. The file type is kind of interesting as the value attribute can only be set by the user selecting a file with other attempts to set it being ignored. This is for security reasons, as otherwise it would be easy to maliciously access the user’s computer.

The file tag allows you to specify what types of files that you want to get from the user by using the accept attribute. These are MIME types but you can use common file extensions which will automatically be mapped to the appropriate file type.

You may also allow the user to request multiple files at the same time by adding the multiple attribute. Here is an example of requesting a file.

<input type="file" id="textfilePicker"
accept=".txt,.js,.html,.css"
onchange="fileDemo()">
<pre id="fileContents"></pre>

The code for handling the file will load the file and put the contents of the file into the file contents pre tag. This is done using some asynchronous methods which we will be discussing in a much later chapter so do not feel discouraged if you do not understand the code but consider this foreshadowing what can be done in modern browsers.

function fileDemo() {
let picker = document.getElementById("textfilePicker");
const selectedFile = picker.files[0];
const reader = new FileReader();
reader.addEventListener("load" , ()=> {
document.getElementById("fileContents").innerText=reader.result;
});
reader.readAsText(selectedFile);
}


            

Next we will be looking at letting users select from a list of options.

Chapter contents

Chapter 5 Contents

5.1 Forms Cheat Sheets

This chapter's summary cheat sheet.

5.2 The client-server model

How forms are traditionally used.

5.3 The Form tag

How to set up a form in HTML.

5.4 Inputting text

Getting text input from the user.

5.5 Specialized text formats

Making your input fields more specific and semantic.

5.6 Form cosmetics

Making forms look more like printed forms.

5.7 checkboxes & radio buttons

How to use Checkboxes and Radio buttons.

5.8 Numbers and Dates

Using numbers and dates gives your user better GUI controls.

5.9 special purpose input types

The reset, button, image, color, and file input types.

5.10 Option Select

The select element lets users select options from a drop-down list.

5.11 Data Lists

Coming October 20th.

5.12 Output

Coming October 27th.

5.13 dialog box concepts

Coming November 3rd.

5.14 using dialog boxes

Coming November 17th.

5.15 Validating forms

Coming November 24th.

5.16 processing forms as an array

Coming December 1st.

5.17 Project: Trivia

Coming December 8th.

5.18 Trivia Project Solution

Coming December 15th.

← previous section
Numbers and Dates
next section →
Option Select
Table of Contents