input (type=button) element

If you don't know what an element is or how you must use it, I recommend you read the "HTML tags and attributes" tutorial that you can find in the HTML tutorials section.

Description

The input element, having the "button" value in its type attribute, represents a button with no predefined action associated to it, which means that by default, the button won't produce any effect when pressed. These buttons are normally used with client-side scripts that are triggered when the user clicks on them.

The value attribute has a particular meaning for this element: it represents the label of the button and is usually render by browsers inside of it.

In contrast with submit and reset buttons that have default labels (used when the value attribute is absent), this type of buttons can have the empty string as label.

Examples

In this example we'll set up a small form with a couple of fields and a button of each type ("submit", "reset" and "button"). As we're not associating the last button with a script, you'll be able to check that it doesn't do anything when pressed.

<form action="../../form-result.php" method="post" target="_blank">
  <p>Name: <input type=""text" name="fullname" value="Jhon Doe"></p>
  <p><label><input type="checkbox" name="over18"> I'm over 18</label></p>
  <p>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    <input type="button" value="I do nothing">
  </p>
</form>

Name:

Now, we're using a button to trigger a script. This script has been defined elsewhere and changes the color of the button's parent element (in this case a p element) when executed.

<p><input type="button" value="I got a job!" onclick="changeColor(this.parentNode)"></p>

Attributes

Specific attributes

autofocus

A boolean value instructing the browser to set the focus to this control when the document has finished loading or when the dialog where the control finds itself is shown. If the attribute has the value "autofocus" or the empty string (""), or if it's just present, the control should get the focus as soon as possible, after the page or dialog has been loaded.

Example

<p><input type="button" value="Change color" autofocus></p>

disabled

A boolean value indicating wether the control is disabled or not. If the attribute takes the value "disabled" or the empty string (""), or if it's just present, the control will be disabled.

Disabled controls are rendered greyed out (if visible), are blocked from user interaction and, more importantly, their values (if any) aren't sent when the form is submitted.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>Nickname: <input type="text" name="nickname"></p>
  <p>
    <input type="submit" value="Submit">
    <input type="button" value="Check availability" disabled>
  </p>
</form>

Nickname:

form

The value of the id attribute of the form with which this control is associated to.

This attribute is new in HTML5 and helps defining the pertenence of controls in nested or distant forms.

Example

<form id="form1" action="../../form-result.php" method="post" target="_blank">
  <p>Product name: <input type="text" name="productname"></p>
</form>
<p><input type="button" value="Check database" form="form1" onclick="checkDatabase()"></p>

name

A name for the control. This name will be sent by the browser to the processing agent, paired with the content of the value attribute. Both attributes together will conform a name-value pair that will be used to process the form data.

Currently, the value isindex, formerly used in a special way by some browsers and included in the HTML standard, isn't permitted in this attribute.

The name-value pair of a button is submitted, with the other form data, only if the button has been used to submit the form.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>Announcement: <input type="text" name="announcement"></p>
  <p>
    <input type="submit" value="Submit">
    <input type="button" name="justabutton" value="I'm just a button">
  </p>
</form>

Announcement:

type

A value indicating the type of the field that this element represents. There are twenty two possible values (case-insensitive):

  • hidden: a hidden control used to send information to the server, typically managed by scripts.
  • text: a control used to input a single-line piece of text.
  • search: same as text but for search purposes.
  • tel: a control used to provide a telephone number.
  • url: a text box used to input a single and absolute URL.
  • email: a control designed to edit one or more e-mail addresses.
  • password: a text box for editing passwords, where the characters are represented by dots.
  • date: a control to input a specific date.
  • month: a control to input a specific month.
  • week: a control to input a specific week.
  • time: a control to input a specific time.
  • datetime-local: a control to input a specific local date and time.
  • number: a control to input a number.
  • range: a control to input one or two numbers inside a range.
  • color: a control to input a color.
  • checkbox: a control to input a boolean value (true/false).
  • radio: a control used to choose one single option among many.
  • file: a control used to upload files to the server.
  • submit: a button used to submit the form.
  • image: same as submit but with the ability to be shown as an image instead of using the default button appearance.
  • reset: a button used to reset the form's controls to their default values.
  • button: a button with no default action associated.

When this attribute isn't present, the element behaves as it would have the value "text".

Example

<p><input type="button" value="Change size" onclick="changeSize()"></p>

value

A value for the control. This value will be sent by the browser to the processing agent, paired with the content of the name attribute. Both attributes together will conform a name-value pair that will be used to process the form data.

Additionally, browsers will use the content of this attribute as a label for the button.

The name-value pair of a button is submitted, with the other form data, only if the button has been used to submit the form.

Processing agents will find little use in the name-value pair passed by a button. Instead, the value attribute has a more important task providing the label that browsers will display as content of the button.

Example

<form action="../../form-result.php" method="post" target="_blank">
  <p>Search: <input type="search" name="searchstring" value="Love"></p>
  <p>
    <input type="submit" value="Search">
    <input type="button" name="donothingbutton" value="I do nothing">
  </p>
</form>

Search:

Global attributes

For information about global attributes refer to this list of global attributes in HTML5.

Events

Global events

For information about global events refer to this list of global events in HTML5.