Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom attributes in html tags [duplicate]

Tags:

html

Possible Duplicate:
Custom attributes - Yea or nay?

Can any side effect occur in browsers if I add custom attributes to html tags for instance:

<a href="" mycustomtag="mydata"></a>
like image 610
user391986 Avatar asked Nov 05 '10 04:11

user391986


People also ask

Can you add custom attributes HTML tags?

By custom attribute, we mean you can add any type of attribute to any given HTML element. If you are just creating a static web page that has nothing other than HTML and CSS, then adding custom attributes to HTML elements is generally not needed and is not necessary.

What is the recommended way to add custom HTML5 attributes?

You can use getAttribute() and setAttribute() in JavaScript to get and set the value of different data attributes. The getAttribute method will either return null or an empty string if the given attribute does not exist. Here is an example of using these methods: var restaurant = document.

Can an attribute be applied multiple times to the same element?

Using the same attribute name twice in a tag is considered an internal parse error. It would cause your document to fail validation, if that's something you're worried about. However, it's important to understand that HTML 5 defines an expected result even for cases where you have a "parse error".

Can tags have multiple attributes?

The multiple attribute in HTML allows user to enter more than one value. It is a Boolean attribute and can be used on <input> as well as <select> element, To allow multiple file uploads in HTML forms, use the multiple attribute. The multiple attribute works with email and file input types.


3 Answers

No side effects that I know if, they just won't validate and it's possible that in the future whatever attribute name you choose may conflict if browsers decide to implement one with the same name (Unlikely if you name it something unique enough)

Read more here: Custom attributes - Yea or nay?

like image 68
DaiYoukai Avatar answered Oct 02 '22 19:10

DaiYoukai


This is fine, except that if you try to validate your markup, this will cause it to fail.

Otherwise, from a browser perspective there shouldn't be a problem.

like image 29
Jacob Relkin Avatar answered Oct 02 '22 18:10

Jacob Relkin


Everyone will be kind to you (JavaScript, CSS, HTML) but your page will not validate.

If you decide to use custom attributes, prefix them with data- to make them forward compatible with HTML5 (and to not use a possible reserved attribute in the future).

<a href="http://example.com" data-myatt="mydata">click?</a>
like image 42
alex Avatar answered Oct 02 '22 19:10

alex