Vaibhav Jain
Vaibhav Jain

Reputation: 34407

Custom attributes in html

How to add a custom attribute to a HTML control.

 <input type="text" validate="xyz"></input>

I want to add a new attribute to the HTML control. validate here is the custom attribute.

Upvotes: 1

Views: 141

Answers (2)

David Thomas
David Thomas

Reputation: 253308

If you're using html5, you can use:

data-customAttributeName="whatever value"

The data- prefix is mandatory, but you can follow it up with anything you like (so long as it's a valid html character string (a-z, 0-9, underscore and hyphen1).

Beyond that, though, it's of little importance since html will ignore attributes it doesn't understand or simply can't parse. Custom attributes will likely, though, throw validation errors (unless you define and specify a custom DTD).


  1. There's probably others, but I can't remember exactly.

Upvotes: 0

Oded
Oded

Reputation: 498934

Just add it.

HTML will ignore attributes it doesn't "know" about, so long as the markup is valid.

Upvotes: 1

Related Questions