Is It Normal to Have Two Elements with Same Id in Two Div Elements with Other Id

Is it normal to have two elements with same id in two div elements with other id?

https://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#adef-id

Sample Image

BUT!

If you need it in your project, you can use it, like suggested in my Question Final Update!

Can multiple different HTML elements have the same ID if they're different elements?

No.

Element IDs should be unique within the entire document.

Can two html elements have the same id but in different classes?

Possible? Yes.
Good coding? No.

From w3.org:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").

Two elements with the same id, want to select one contained in a specific div

I know you haven't tagged this with jQuery - but I think it could be a solution. So what about giving every div a class, and then run an each-loop with jQuery?

<div id='container0' class='container'>
<input id='item0' class='item'>
</div>

<div id='container1' class='container'>
<input id='item0' class='item'>
</div>

Then I think an each loop could handle it like this:

$('div.container').each(function(key, value) { 
var elmid = $("#"+key + " div.item).attr('id');
var containerid = $(this).attr('id');

var newelmid = elmid.replace('item','');
var newcontainerid = elmid.replace('container','');

if(newelmid != newcontainerid)
{
$("#"+elmid).attr('id', 'item'+newcontainerid);
}

});

Let me know if that is anyway near what you want to do :)

Multiple divs with the same id invalid?

Yes, it's invalid to have multiple divs with the same id.

Using a class should work fine:

div.details {
width: 698px;
background-color: #FFC;
}

If those rules really get overridden, you probably have another rule in place that has higher specificity. In that case, you would have to show us more of your HTML.

Can multiple different HTML elements have the same ID if they're different elements?

No.

Element IDs should be unique within the entire document.

Can an HTML element have multiple ids?

No. From the XHTML 1.0 Spec

In XML, fragment identifiers are of
type ID, and there can only be a
single attribute of type ID per
element. Therefore, in XHTML 1.0 the
id attribute is defined to be of type
ID. In order to ensure that XHTML 1.0
documents are well-structured XML
documents, XHTML 1.0 documents MUST
use the id attribute when defining
fragment identifiers on the elements
listed above. See the HTML
Compatibility Guidelines for
information on ensuring such anchors
are backward compatible when serving
XHTML documents as media type
text/html.



Related Topics



Leave a reply



Submit