The 5 Useful HTML Attributes...

no img

Author : Narhari Kale.

Published On : 11-Aug-2021


In this blog, we are learning about 5 HTML Attributes that will be helpful for you. HTML elements have attributes, there are dozens of attributes are available in HTML but we are learning only a few of them.

What are attributes? Attributes are like extra features for elements or we say that it can adjust elements default behavior. It can be written in name and value pair e.g.name: "value"

We are covering the following attributes.

  • type
  • name
  • for
  • value
  • required
  • Let's get started.


    type

    it is used to specify the type of element. In the case of the input element, we can use type to choose between various values like number, text, time, date, etc. It is commonly used with input,button,style,script elements.

    no img

    In the above example,type = "number" gives you an output with the number field and type="radio" gives you the radio button.

    name

    name attribute can be used to store the name of that HTML element. It can also be used as a reference element to javascript. When we use the input element with type radio, it is used to form a group of radio inputs

    no img

    in this case, we used a label with an input element. Input element has a type of radio button, which forms a group of two input elements so we can toggle with them and answer that question with true or false. If we change the second input element name with a different name it acts as a separate radio button.

    for

    for attribute is used to connect elements. We can use for attribute with label and output elements.

    no img

    Here we used for attribute to connect label with input element. it works like this --> this is a label for input elements with id "radio1", the value of for attribute has to be same as the id of an input element.

    value

    It specifies the value for that element. value attribute is used as the default or initial value of that element. It can be used with button and input elements.

    no img

    In the above example initial value of input element is yes. We can access this value in javascript to modify or compare values.

    required

    As its name suggests it is required. it used with input,textarea,select elements. it indicates the user has to fill out the input element.

    no img

    It is used to show user, a prompt that you did not fill the input element, and it's mandatory to fill that out. The required attribute is a boolean attribute.


    That's it. hope you have enjoyed👍. this is my first blog, so please share your valuable feedback 🤗, it will be very helpful for me.