Custom Attributes in HTML elements - html

My question is simple
How many custom attributes we can use in a element e.g
<input value="1" vcFlag="true" name="example" />
I am using vcFlag="true" as custom attribute how many attributes like this I can put in this input element. Is there any limit or not.
thanks

There is no (theoric) limit to the number of attributes you can add to a HTML tag:
Every HTML element may have any number of custom data attributes specified, with any value.
Attribute parsing is slower than node parsing so if you use a huge number of custom attributes you may slow down page parsing, specially on some old browsers.
Just rmember to prefix them with data- to be HTML 5 compatible: Data Attributes on W3C Working Draft.
In your case it should be:
<input value="1" data-vcFlag="true" name="example" />

There is no limit for custom attributes.
Thanks to HTML5, we now have the ability to embed custom data attributes on all HTML elements, these new custom data attributes consist of two parts:
Attribute Name :
The data attribute name must be at least one character long and must be prefixed with 'data-' and should not contain any uppercase letters.
Attribute Value :
The attribute value can be any string.
HTH

Related

Can a HTML element have multiple unique ID attributes? [duplicate]

This question already has answers here:
Can an HTML element have multiple ids?
(18 answers)
Closed 8 years ago.
Needed to know if an HTML element can have multiple attributes of ID's, for instance :
<input type="text" id="identifier1" id="selector1" />
As I needed to clarify this statement mentioned about selectors at W3 website.
If an element has multiple ID attributes, all of them must be treated
as IDs for that element for the purposes of the ID selector. Such a
situation could be reached using mixtures of xml:id, DOM3 Core, XML
DTDs, and namespace-specific knowledge.
The Possible duplicates which people are referring, states question for this syntax
<input type="text" id="identifier1 selector1" />
which is different than syntax that I am asking.
Needed to know if an HTML element can have multiple attributes of ID's
Short answer? No because the browser will only render the first one.
See this Fiddle, I can only target it in CSS using the first id that appears in the DOM. Try changing that CSS selector to use the second id, it won't work.
That's because it seems like the second ID is being disregarded by the browser, as this is the output HTML:
<input type="text" id="identifier1">
If you really need additional identifiers on an element, you should think about using either multiple class names or data attributes to correspond to additional data.
Needed to know if an HTML element can have multiple attributes of ID's
No. No element in HTML may have more then one instance of a given attribute.
As I needed to clarify this statement
Note the last sentence in that statement.
Also note that the CSS idea of an "ID attribute" is not "An attribute with the name id". Also quoting from that document:
Document languages may contain attributes that are declared to be of type ID
Only the id attribute is an ID type in HTML.
No, even if you specify multiple ids, the first encountered id attribute is used.
No ID can not be same for html elements, but the Classes are to use for multiple elements, and one element may have multiple classes
No, because an attribute must not be repeated in tag. This is a general rule in HTML, not limited to the id attribute. For nominally SGML-based versions of HTML and for XHTML, this follows from general SGML and XML rules. For HTML serialized HTML5, see HTML5 CR, 8.1.2.3 Attributes.
It’s difficult to see why you would use duplicate id attributes, so I can’t suggest a workaround. In general, for any normal use of the id attribute, one attribute per element is sufficient.
No. Element IDs should be unique within the entire document.

Firefox: tag attributes badly reordered after html parse

HTML code:
<input id="txtSizeBeforeValue" type="text" size="5" value="blabla">
<input id="txtValueBeforeSize" type="text" value="blabla" size="5">
HTML parsed code:
<input id="txtSizeBeforeValue" type="text" value="blabla" size="5">
<input id="txtValueBeforeSize" type="text" size="5" value="blabla">
Here you can see there is an issue with Firefox HTML parser, tag attributes are badly reordered with every HTML tag like input text/button, textarea, canvas...
Even if you write tag attributes like it is reordered, it is reordered again.
The problem is visible in Firebug and the HTML analyse developer tool of Firefox so it can't be a Firebug issue.
No problem with Google Chrome since the HTML parser doesn't reorder tag attributes.
Do you have this issue with Firefox too? On every platform?
You should blame the DOM for the reordering instead of blaming the parser. (I wrote the HTML parser in Firefox.)
In Firefox, different attributes are stored in different representations internally (particularly attributes that are treated as legacy presentational hints for CSS purposes). When you see attributes reordered, it means you have attributes belonging into different kinds of internal storage buckets on one element.
You'll find that the order of attributes you can read from IE's DOM also often isn't the same as the source order.
This is OK. The spec just requires the order to be stable, so if you read innerHTML twice without changing the attributes in between, you should get the same serialization twice.
While the exact order of attributes is UA-defined, and may depend on factors such as the order that the attributes were given in the original markup, the sort order must be stable, such that consecutive invocations of this algorithm serialize an element's attributes in the same order.
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#serializing-html-fragments
HTML spec states that "They may appear in any order."
here: http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
This is not a bug!
However you can create your own tool...
The order of attributes doesn't matter as long as their values are assigned correctly

What is the use of name, id and value?

Why do we use the the name, id and value attributes with html elements? What are they important and how are they interpreted? What are the differences between them? I have looked at w3schools and every tutorial but I would like a simple explanation from a person.
What is the difference between just doing:
<form>
<input type="text" />
</form>
and
<form>
<input type="text" name="name" />
</form>
what are the benefits of using these attributes?
name - passed to the server in forms
id - a unique identifier to the HTML element
value - the value of an input or textarea element
The presence of a name attribute in an input element causes a name=value pair to be included in the form data, if the value is not empty. In the absence of such an attribute, the form field does not make any contribution to the form data.
The id attribute can be used to give an element a unique identifier that can be used in client-side scripting and styling. It has nothing to do with the functionality of the name attribute.
The value attribute in a text input box specifies the initial (default) content of the input field.
Each form element in your application must save some information for you. Those are value.
When you want process your forms using a server-side programming language, you must point to your wanted element. Here, you need name to fetch your form elements values.
Also, sometimes you might need to process your form client-side or do something else on elements in your HTML document, now one way to point to them can be an id.
id is usually referred to by, or used in relation to, CSS styling. name is usually referred to by data-related php or or other server-side scripting , and value is the "content" ascribed to that element, so if input value = "hello", then that is what will appear in the text input field.
One point that the other answers don't make clear, is that the purpose of an attribute can differ depending on which element it belongs to.
So while an id attribute identifies an element no matter where it is, the name attribute serves a different purpose on the iframe and object than it does on the meta element, which is again different from its purpose on the submittable elements button, input, keygen, object, select and textarea. The param element and the map element both have name attributes, each for a different purpose, while the form element, fieldset element and output element use their name attributes for a more or less common purpose, but different from the other elements.
Similarly, the value attribute on the input, button and option elements serve similar but slightly different purposes, and the progress and meter elements share a similarly purposed value attribute, but each of the param, li, and data (WHATWG HTML living standard only) elements has a value attribute with a purpose dedicated to that particular element.
To understand all the purposes properly, I recommend that you at least read the spec.

Name vs Id attribute in HTML

Are there any advantages to using <div id="here" ... instead of <div name="here" ...?
Are they both referenced to as #here?
Here are some differences between both:
name has never been a div element attribute.
name as used on the form controls (input, textarea, select, and button elements) is radically different from the id attribute on named elements. In this case, the name attribute relates to how data is labeled when sent to server, and multiple elements may share the same name.
The id attribute on the other hand is for identifying one unique element for the purposes of scripting, styling, or addressing.
The use of the name attribute on other elements than form controls was in HTML 4.01 the same as that of id, but it allowed for a wider set of characters than the id attribute and wasn't controlled in quite the same way. Because of this ambiguity the W3C decided to deprecate/remove the name attribute on those elements in favor for the unambigous id attribute in XHTML.
This is also connected to another detail of XML however - only one attribute of any element may be of the type ID, which would not be the case if they let name stay on the element, but corrected the ambiguity problems.
As the name attribute didn't work the same on those two sets of elements, it was best to remove one of them.
In short, for backwards compatibility, you should use the name and id attribute both, both set to the same value, for all elements except form controls if you use HTML 4.01 or XHTML 1.0 Transitional. If you use XHTML 1.0 Strict or later you should use only id. For form controls, you should use name for what you want the form to send to the server and for DOM 0 access, and only use id for styling, DOM1-3 access or addressing reasons
It depends on where you are going to use them.
Usually, id of an element is unique while multiple elements can share the same name.
Id is referenced as #here, and name is referenced as [name=here].
They are not interchangeable, even if they sometimes appear to be.
Name should only exist on form input fields. It is what that tag will cause the browser to pass in the submission as the name of the field. As Tomalak noted in a comment, DIV actually does not have a Name attribute.
ID is the unique identifier for the DOM, to manipulate or refer to the tag; for example, in JavaScript.
The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element.
Check div element.
You are probably thinking about the input tag. It offers the name attribute to identify which input is what when the form is submitted.
id would be only used to style the input, whereas name would not.
One thing of importance that hasn't been mentioned in the other answers is that a form with a name attribute gets added as a variable to the document object in JavaScript. Any form controls with a name inside that form are accessible as child objects on the form element.
So if, for example, you had some HTML like the following:
<form name="myForm">
<input name="myInput" type="text" />
</form>
You could access the form with document.myForm or access the input element with document.myForm.myInput.
See an example of this idea in action on JSFiddle.
The answer is:
the id attribute is to be unique across the entirety of the HTML document, and
the name attribute has no such requirement.
By convention, the name value is used with forms to help with the processing of forms, particularly with radio buttons. How you use the name attribute depends on your use case. If you need some other tag for your use case, you can of course create a new attribute and use that as you will; pick a name for your attribute that will likely be very unique!

Difference between id and name attributes in HTML

What is the difference between the id and name attributes? They both seem to serve the same purpose of providing an identifier.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
The name attribute is used when sending data in a form submission. Different controls respond differently. For example, you may have several radio buttons with different id attributes, but the same name. When submitted, there is just the one value in the response - the radio button you selected.
Of course, there's more to it than that, but it will definitely get you thinking in the right direction.
Use name attributes for form controls (such as <input> and <select>), as that's the identifier used in the POST or GET call that happens on form submission.
Use id attributes whenever you need to address a particular HTML element with CSS, JavaScript or a fragment identifier. It's possible to look up elements by name, too, but it's simpler and more reliable to look them up by ID.
Here is a brief summary:
id is used to identify the HTML element through the Document
Object Model (via JavaScript or styled with CSS). id is expected
to be unique within the page.
name corresponds to the form element and identifies what is posted
back to the server.
The way I think about it and use it is simple:
id is used for CSS and JavaScript/jQuery (it has to be unique on a page).
name is used for form handling on the server when a form is submitted via HTML (it has to be unique in a form - to some extent, see Paul's comment below).
See id= vs name=:
What’s the difference? The short answer is, use both and don’t worry about it. But if you want to understand this goofiness, here’s the skinny:
id= is for use as a target like this: <some-element id="XXX"></some-element> for links like this: <a href="#XXX".
name= is also used to label the fields in the message send to a server with an HTTP (HyperText Transfer Protocol) GET or POST when you hit submit in a form.
id= labels the fields for use by JavaScript and Java DOM (Document Object Model).
The names in name= must be unique within a form. The names in id= must be unique within the entire document.
Sometimes the name= and id= names will differ, because the server is expecting the same name from various forms in the same document or various radio buttons in the same form as in the example above. The id= must be unique; the name= must not be.
JavaScript needed unique names, but there were too many documents already out here without unique name= names, so the W3 people invented the id tag that was required to be unique. Unfortunately older browsers did not understand it. So you need both naming schemes in your forms.
Note: attribute "name" for some tags like <a> is not supported in HTML5.
The ID tag - used by CSS, define a unique instance of a div, span or other elements. Appears within the JavaScript DOM model, allowing you to access them with various function calls.
The Name tag for fields - this is unique per form -- unless you are doing an array which you want to pass to PHP/server-side processing. You can access it via JavaScript by name, but I think that it does not appear as a node in the DOM or some restrictions may apply (you cannot use .innerHTML, for example, if I recall correctly).
Generally, it is assumed that name is always superseded by id. This is true, to some extent, but not for form fields and frame names, practically speaking. For example, with form elements, the name attribute is used to determine the name-value pairs to be sent to a server-side program and should not be eliminated. Browsers do not use id in that manner. To be on the safe side, you could use the name and id attributes on form elements. So, we would write the following:
<form id="myForm" name="myForm">
<input type="text" id="userName" name="userName" />
</form>
To ensure compatibility, having matching name and id attribute values when both are defined is a good idea. However, be careful—some tags, particularly radio buttons, must have nonunique name values, but require unique id values.
Once again, this should reference that id is not simply a replacement for name; they are different in purpose. Furthermore, do not discount the old-style approach, a deep look at modern libraries shows such syntax style used for performance and ease purposes at times. Your goal should always be in favor of compatibility.
Now in most elements, the name attribute has been deprecated in favor of the more ubiquitous id attribute. However, in some cases, particularly form fields (<button>, <input>, <select>, and <textarea>), the name attribute lives on, because it continues to be required to set the name-value pair for form submission. Also, we find that some elements, notably frames and links, may continue to use the name attribute, because it is often useful for retrieving these elements by name.
There is a clear distinction between id and name. Very often when name continues on, we can set the values the same. However, id must be unique, and name in some cases shouldn’t—think radio buttons. Sadly, the uniqueness of id values, while caught by markup validation, is not as consistent as it should be. CSS implementation in browsers will style objects that share an id value; thus, we may not catch markup or style errors that could affect our JavaScript until runtime.
This is taken from the book JavaScript - The Complete Reference by Thomas-Powell.
<form action="demo_form.asp">
<label for="male">Male</label>
<input type="radio" name="sex" id="male" value="male"><br>
<label for="female">Female</label>
<input type="radio" name="sex" id="female" value="female"><br>
<input type="submit" value="Submit">
</form>
The forum thread below has answers to the same basic question, but basically, id is used for scripting identification and name is for server-side.
id vs. name attribute for HTML controls
name is deprecated for link targets, and invalid in HTML5. It no longer works at least in the latest Firefox (v13). Change <a name="hello"> to <a id="hello">.
The target does not need to be an <a> tag. It can be <p id="hello"> or <h2 id="hello">, etc. which is often cleaner code.
As other posts say clearly, name is still used (needed) in forms. It is also still used in META tags.
name vs. id
name
Name of the element. For example used by the server to identify the
fields in form submits.
Supporting elements are <button>, <form>, <fieldset>, <iframe>,
<input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>,
<meta>, and <param>
Name does not have to be unique.
id
Often used with CSS to style a specific element. The value of this
attribute must be unique.
Id is a global attribute. Global attributes can be used on all elements, though the attributes may have no effect on some elements.
Must be unique in the whole document.
This attribute's value must not contain white spaces, in contrast to
the class attribute, which allows space-separated values.
Using characters except ASCII letters and digits, '_', '-' and '.'
may cause compatibility problems, as they weren't allowed in HTML 4.
Though this restriction has been lifted in HTML 5, an ID should start
with a letter for compatibility.
<body>
<form action="">
<label for="username">Username</label>
<input type="text" id="username" name="username">
<button>Submit!</button>
</form>
</body>
As we can see here, "id" and "for" elements are interconnected. If you click on the label (Username) then the input field will be highlighted (this is useful for mobile users and is considered as a good practice).
On the other hand, the "name" element is useful while submitting the form. Whatever you enter in the input field it will be displayed on the URL. Please see the attached image.
The ID of a form input element has nothing to do with the data contained within the element. IDs are for hooking the element with JavaScript and CSS. The name attribute, however, is used in the HTTP request sent by your browser to the server as a variable name associated with the data contained in the value attribute.
For instance:
<form>
<input type="text" name="user" value="bob">
<input type="password" name="password" value="abcd1234">
</form>
When the form is submitted, the form data will be included in the HTTP header like this:
If you add an ID attribute, it will not change anything in the HTTP header. It will just make it easier to hook it with CSS and JavaScript.
ID is used to uniquely identify an element.
Name is used in forms. Although you submit a form, if you don’t give any name, nothing will will be submitted. Hence form elements need a name to get identified by form methods like "get or push".
And only the ones with the name attribute will go out.
If you're not using the form's own submit method to send information to a server (and are instead doing it using JavaScript) you can use the name attribute to attach extra information to an input - rather like pairing it with a hidden input value, but it looks neater because it's incorporated into the input.
This bit does still currently work in Firefox although I suppose in the future it might not get allowed through.
You can have multiple input fields with the same name value, as long as you aren't planning to submit the old fashioned way.
Id:
It is used to identify the HTML element through the Document Object Model (DOM) (via JavaScript or styled with CSS).
Id is expected to be unique within the page.
Name corresponds to the form element and identifies what is posted back to the server.
Example:
<form action="action_page.php" id="Myform">
First name: <input type="text" name="FirstName"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="LastName" form="Myform">
In all the time this question has been around, I am chagrined (and perhaps a bit saddened) that nobody has thought to mention accessibility which, though always important, has been steadily gaining support amongst both management and software engineers (just from my personal observations; no hard data to back that up).
One statistic I can provide is this (source):
So awareness of accessibility shortcomings show a steadily growing trend. The same reference mentions that, from those numbers, one can observe that at least one lawsuit is filed every hour!
So how does accessibility weigh in on name vs id?
According to the World Wide Web Consortium (W3C):
The for attribute of the label must exactly match the id of the
form control.
Based on personal experiences and according to the W3Schools description for attributes:
ID is a global attribute and applies to virtually all elements in HTML. It is used to uniquely identify elements on the Web page, and its value is mostly accessed from the frontend (typically through JavaScript or jQuery).
name is an attribute that is useful to specific elements (such as form elements, etc.) in HTML. Its value is mostly sent to the backend for processing.
HTML Attribute Reference
Below is an interesting use of the id attribute. It is used within the <form> tag and used to identify the form for <input> elements outside of the </form> boundaries so that they will be included with the other <input> fields within the form.
<form action="action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="lname" form="form1">
There is no literal difference between an id and name.
name is an identifier and is used in the HTTP request sent by the browser to serve as a variable name associated with data contained in the value attribute of the element.
The id on the other hand is a unique identifier for browser, client side and JavaScript. Hence the form needs an id while its elements need a name.
id is more specifically used in adding attributes to unique elements. In DOM methods, Id is used in JavaScript for referencing the specific element you want your action to take place on.
For example:
<html>
<body>
<h1 id="demo"></h1>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
Same can be achieved by name attribute, but it's preferred to use id in a form and name for small form elements like the input tag or select tag.
Both name and id is targetable by # so not sure why ID was mentioned for this task exclusively.
I often Inspect those attributes to create specific links to bookmark (where clicking on header with mouse cursor to do the same is not provided for some reason) such as the Option File Inclusions
section of MySQL 5.6 4.2.2.2 Using Option Files documentation:
https://dev.mysql.com/doc/refman/5.6/en/option-files.html#option-file-inclusions
where it's defined as <a name="option-file-inclusions"></a> (with absolutely no forms involved).
I think, the name attribute is also older than id in HTML.
The id will give an element an id, so once you write real code, (like JavaScript) you can use the id to read elements. The name is just a name, so the user can see the name of the element, I guess.
Example:
<h1 id="heading">text</h1>
<script>
document.getElementById("heading"); // Reads the element that has the id "heading".
</script>
// You can also use something like this:
document.getElementById("heading").value; // Reads the value of the selected element.