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

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.

Related

HTML5 best practices issue

So, I've read recently some posts about HTML5 best practices and all of them have the following practice:
"Use id attribute instead of name attribute"
Is that right? I mean, how am I going to handle forms in PHP, for example, if not by inputs' name attribute?
Look here for reference:
Difference between id and name attributes in HTML
https://teamtreehouse.com/community/what-is-the-difference-between-id-and-name-attributes-in-form-elements
Names can be used for more elements, while ids are unique. This helps with styling multiple elements without repeating your css code.
Long story short, id is not meant for naming form elements.
You use id to make it easier for CSS or JavaScript to handle that one particular element with the unique id.
Whether you're using CSS or JavaScript/jQuery with your forms, an ID is handy for most inputs/elements because you can reference them like easily. Remember, ID's are unique:
<div id="example1"></div>
If you have an element like I mentioned above, you can always call it later by doing this:
var x = document.getElementById("example1").id;
along with many other ways.
Names can sometimes be tied to a variety of elements on the page, whereas there will always only by one item with that particular ID.

Are there two ways to jump to a fragment identifier in HTML?

I always thought the standard way to specify a fragment identifier is by <a name="foo"></a>.
go to foo
<a name="foo"></a> <!-- obsolete method, it seems -->
<p>some content under that anchor with name</p>
But it seems like this is the old way, and the new way is using an id, like this:
go to bar
<p id="bar">some content under that p with id</p>
In fact, the W3C validator says that name is obsolete for the <a> element. So are there 2 ways to jump to the fragment identifier but 1 of them is obsolete? (And when did that happen?)
(there are other questions about the difference between id and name, but this one is about fragment identifier)
So are there 2 ways to jump to the fragment identifier but 1 of them is obsolete?
There are two ways to identify a fragment.
(There are also two ways to jump to one, since you can do it with a URL or write a pile of JavaScript to scroll the page).
And when did that happen?
id was introduced in 1996 when HTML 4 came out. It effectively obsoleted the name attribute for anchors.
name was made officially obsolete in HTML 5 in 2014 (or in Living HTML on some date that I'm not going to try to figure out).
Yes there are two ways to jump to a fragment identifier and both aren't obsolete ( except a element).
That's rules applied to all HTML 5 elements other than a (because in a hasn't name attribute in HTML5).
So shortly it's obsolete to idenfity name attribute as fragment idenitifier for a element as that's attribute depricated since HTML4.
Flow of accessing fragment from HTML5 Specification:
If there is an element in the DOM that has
an ID exactly equal to fragid, then the first such element in tree
order is the indicated part of the document; stop the algorithm here.
If there is an a element in the DOM that has a name attribute whose
value is exactly equal to fragid, then the first such element in tree
order is the indicated part of the document; stop the algorithm here.
Otherwise, there is no indicated part of the document.
Both ways of doing fragment identifiers work.
Using id="fragment" is the newer, recommended way of jumping to fragments in HTML. It was introduced with HTML4, and works basically everywhere (I just verified this with IE5).
<a name="fragment">, the older way, still works, but is obsolete since HTML5.
Answer to your question: Yes, There are two ways to identify a fragment and one is obsolete.
What is Fragment Identifiers ?
Fragment identifiers for text/plain.
URIs refer to a location in the same resource. This kind of URI starts with "#" followed by an anchor identifier (called the fragment identifier).
Fragment Identifier using JS like below.
location.replace('#middle');
More information on the name attribute.
Basically, the name attribute has been deprecated (obsolete in HTML5-speak) for just about everything except for form elements. Forms retain them as the method of identifying data, and it is the name plus the value property which is sent back to the server. (The id in form elements is used for attaching label elements, and has nothing to do with the actual data).
There is a fundamental weakness in the name attribute, which the id attribute addresses: the name attribute is not required to be unique. This is OK for forms where you can have multiple elements with the same name, but unsuitable for the rest of the document where you are trying to uniquely identify an element.
The id attribute was specifically required to be unique, which makes it better for identifying a link target, among other things. CSS is pretty relaxed about applying styles to multiple elements with the same id, but JavaScript is more strict about this requirement. And, of course, you can’t have a practical link target if you can’t guarantee uniqueness.

CSS ID and class doesn't show any difference [duplicate]

This question already has answers here:
Several elements with the same ID responding to one CSS ID selector
(3 answers)
Closed 9 years ago.
I know the differences between ID and Class. But why both IDs take effect in this html? ID should be used uniquely, right?
#text {
text-align:center;
color: red;
}
Then id="text" was used twice in my same html page, and both get same effect. Why do i have to use "class", if 'id' has same effect?
That IDs seem to behave like classes when you have duplicate IDs in a page is nothing more than a side effect of how browsers implement CSS.
Whether a document conforms to its own rule that IDs should be unique is not relevant to CSS. CSS simply says to match elements that have a specific value in its ID attribute, to an ID selector:
An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector.
It does not say anything about what should happen if there is more than one element with such an ID, because it assumes that you are working with a conforming HTML document.
You should use classes to group your elements because that's what HTML says to do, and you are trying to conform to valid HTML. Having duplicate IDs is quite simply not allowed by the HTML spec, and can result in unexpected behavior, so it is not something to be relied on regardless of what effect it has in browsers.
Yes, 'id' have the same effect like 'class'. But there is some different between them.
1. The 'id' selector can't be repeat in the same page. That means when there is a id selector
named "header",there can't be another "header" in this page.This is for js dom command.
2. The 'id' selector have a higher priorty. That means when you have a markup like this:
<style>
#header{color:red;}
.header{color:blue;}
</style>
<div id="header" class="header">
test
</div>
the color in test is "red". http://www.impressivewebs.com/difference-class-id-css/
the DOM will not fail to load with duplicate IDs, but you will have negative effects with other aspects, like javascript. If you are only using ID to identify where CSS rules should apply then they will work just like Class.
In general, ID should always be unique on the page, but the DOM will not enforce this.
IDs may work for css in some browsers, but not in others. There may also be bugs and side effects, for example:
<div>
<input type="radio" id="id1" name="r1" />
<label for="id1">this is radio label for id1</label>
</div>
<div>
<input type="radio" id="id1" name="r1" />
<label for="id1">this is another label for id1</label>
</div>
<!--the second label will also trigger the first radio change -->
Sometimes the functions won't be bind to your radio. Just keep IDs unique on the page.

Why should one add ID to their HTML tags?

A simple question: why should we add the id into our HTML tags if they work perfectly well without them? I know that one of their uses is being able to navigate though the page via hashtags (#), but is there any other use for them?
Uses of id attributes in HTML
As a target for a fragment identifier on a URL.
As a target on form controls for the for attribute on <label> and <output> elements.
As a target on <form> elements for the form attribute on form associated elements.
As a target for element references via the microdata itemref attribute.
As a target for element references via some ARIA attributes including aria-describedby, aria-labelledby and 4 others.
As a target on <th> elements for the headers attribute on <td> and <th> elements.
As a target on <menu> elements for the contextmenu attribute.
As a target on <datalist> elements for the list attribute on <input> elements.
As part of a hash-name reference to <map> elements for the usemap attribute on the <img> and <object> elements.
As an identifier of an element in a CSS selector
As an identifier of an element for JavaScript processing
They're most often used to uniquely identify elements for styling (CSS) and scripting (JavaScript et al) purposes.
But if you're asking about HTML and only HTML, then one example where declarative IDs are useful is associating a <label> with its <input>, <button> or <textarea> control via its for attribute:
<label for="ex">Example field:</label>
<input type="text" name="ex" id="ex">
Without assigning this attribute, activating the label does nothing, but when you pair both elements together using for and id, activating the label causes its control to gain focus.
The other way to associate a form label with its control is to contain it within the label:
<label>
Example field:
<input type="text" name="ex">
</label>
But this doesn't always suit the structure of a form or a page, so an ID reference is offered as an alternative.
Other circumstances where an id attribute serves a function are covered extensively in Alohci's answer.
You can use IDs to acces your divs from javascript, CSS and jquery. If you don't use IDs it will be very difficult for you to interact with your HTML page from JS.
AFAIK, they are used to uniquely refer to a tag.And makes it easier for you to refer to the tag.
IDs are used for accessing your elements in CSS and JavaScript. Strictly speaking IDs should uniquely identify an element. You can also use class attributes to identify groups of elements.
The id attribute provides a unique identifier for an element within the document. It may be used by an a element to create a hyperlink to this particular element.
This identifier may also be used in CSS code as a hook that can be used for styling purposes, or by JavaScript code (via the Document Object Model, or DOM) to make changes or add behavior to the element by referencing its unique id.
see http://reference.sitepoint.com/html/core-attributes/id
for more info on class see here: http://reference.sitepoint.com/html/core-attributes/class
it is there to help you identify your element in java-script code.the getElementByID function in java-script give the handle of an element with specific ID for you.like this.
var someElement = document.getelementById("someID");
// do whatever with someElement;
I myself also prefer class for styling through CSS but sometimes you need an element to be unique. For accessibility reasons you use id to input elements to "connect" its label to it by using for attribute. And for Javascript it's much simpler to select an element if it has got id attribute.
The main reason I use ids for my HTML elements is the fact that their selection is faster, in Javascript with getElementById and in CSS as well, using the #id class.
Of course, I'm not saying this is always a good idea, especially in CSS, where having classes based on ids can cause a lot of redundancy, it's just one of the reasons
First, only add ID when you will need to use them. In most cases id is used to do other things like:
A reference for scripts,Selecting elements to apply scripts to,
A style sheet selector, selecting elements for styling
Named anchors for linking to, which is what u called page navigation
So simply because in most cases you will want to do something to or with your content in any tag, its good to put an identifier, that is the id attribute.

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!