devxlogo

Adding Custom Attributes to HTML Elements

Adding Custom Attributes to HTML Elements

A custom attribute is any user-defined attribute, just like other primitive HTML element attributes. You can add custom attributes to any HTML element. For example, suppose you wanted to add the displayName attribute to the following HTML text box:

    <input type="text" id="txtBox" />

There are two ways to do this:

  1. You can add displayName when you write the HTML element tag in the web page:
         
  2. You can use the DOM:
         document.all.txtBox.displayName = "displayNAME1";

As you can see, you access custom attributes the same way you’d access any built-in attributes. For example:

document.getElementById("txtBox").getAttribute("displayName");
See also  lenso.ai - an AI reverse image search platform
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist