-3

I have as simple webpage where dates and names are input into form fields and fields and transmitted in context in paragraphs when the user clicks a button. Some are output to spans, using InnerHTML others to inputs.

Some dates/names must appear multiple time in the document. I need to my code to apply to apply to all boxes with that actual ID. Should I instead be using a class?

Example this outputs a date:

// This Defines the Date the Medical Was received
  var MEDRName = document.getElementById("MEDRName");
var elements = document.getElementsById("OUTMEDR");
for(var i = 0, len = elements.length; i < len; i++){
elements[i].onclick = function () {
  var OUTMEDR = document.getElementById("OUTMEDR");
  var name = MEDRName.value;

This appears in multiple paragraphs, and I need each element with this ID to populate.

<p> The medical was signed by <input type="text" id="MEDRNAME" /> </p>

<p><input type="text" id="MEDRNAME" /> indicated that yadayadayada</p>
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Ojay
  • 51
  • 8

1 Answers1

1

I need to my code to apply to apply to all boxes with that actual ID

Id should be unique. Since there should be only one element with an id, getElementsById isn't valid, but getElementById is.

You can see more info here.

Should I instead be using a class?

Yes, you should and it's the appropriate way to do it. And then you can use getElementsByClassName or querySelectorAll to select them.

d3c0d3d
  • 76
  • 4