0

I create with JavaScript / JQuery IDs, which I can output afterwards with HTML. Problem is that I can output each ID only once. How can I implement that the ID can be output as often as I want? I have created an example where I want to output the ID output 2 times.

Here is an example of what I mean:

if($('#output').length){
    document.getElementById('output').innerHTML = 'Example output';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output">Load</div>
<p>Here is more page content</p>
<div id="output">Load</div>

I also created a fiddle if it helps more. https://jsfiddle.net/dfaLpv0h/

Paeddy
  • 71
  • 4
  • dom id is unique, you can use class. – glovemobile Mar 03 '21 at 11:59
  • 2
    id attributes *have* to be unique in the DOM. Use a common class on multiple elements instead, but remember you'll need to loop through them all. – Rory McCrossan Mar 03 '21 at 12:00
  • 1
    `$("[id=output]")` - but much better to not have duplicate IDs - even Chrome now complains about these in the console. – freedomn-m Mar 03 '21 at 12:05
  • You can have multiple `id`s in certain circumstances. [w3c id-attribute](https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#the-id-attribute): "This specification doesn't preclude an element having multiple IDs, if other mechanisms (e.g. DOM Core methods) can set an element's ID in a way that doesn't conflict with the id attribute." – tom Mar 03 '21 at 12:06
  • @kmgt nice link - it says "an element [can have] multiple IDs" - not "multiple elements can have the same ID" which it precludes by status it's the "elements unique identifier". I guess it means `
    ` is ok, but still `
    ` is not ok.
    – freedomn-m Mar 03 '21 at 12:11
  • @freedomn-m I guess you are right. But how to manipulate the DOM and append another `id`!? And how to work with a second `id` attribute... The definition makes no sence. – tom Mar 03 '21 at 13:38

0 Answers0