views:

20

answers:

2

How to add a custome 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 cutome attribute.

+1  A: 

Just add it.

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

Oded
A: 

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.
David Thomas
some related question and solution worth reading: http://stackoverflow.com/questions/992115/custom-attributes-yay-or-nay/992464 and http://stackoverflow.com/questions/209428/non-standard-attributes-on-html-tags-good-thing-bad-thing-your-thoughts
bangbambang