HTML button Tag name Attribute Tutorial

In this section, we will learn what name attribute is and how it works.

What is name Attribute in HTML <button> Tag?

The name attribute is used to set a name for the target <button> element.

Using this name thus allows us to reference the element in languages like JavaScript, for example.

In case of JavaScript, there’s a method named `getElementsByName()` which accepts a name value and it will return a list of all elements that contain this name as the value of the `name` attribute.

Also, this name can be used in servers to access the value of this element.

HTML name Attribute in <button> Tag Syntax:

<button name = "name"> </button>

<button> Tag name Attribute Values

The value we set to the name attribute could be any name wrapped with a pair of double quotation.

Example: using name attribute in HTML <button> tag

See the Pen using name attribute in HTML <button> tag by Omid Dehghan (@odchan1) on CodePen.

In this example, we’ve set the value of the name attribute to “pink” and then in the JavaScript source code, used the `getElementsByName()` method to get a reference access to the button and then changed its background color with the help of backgroundColor property.


Top Technologies