Appending <span> tag separately to every Array value using Javascript

appending meaning
append antonym
appended synonym
append in a sentence
append python
append signature
appending death
appending data

This is my first JavaScript project and I am having some difficulties to append the <span> tag separately to each value of an array. Whatever I tried so far results in a single <span> tag. What could be an elegant solution using native JavaScript? Thanks in advance for any help.

let numbers = [1990, 1991, 1992];

let history = [];
let historyContainer = document.getElementById("output");
let para = document.createElement("p");
history.push(numbers.join(" "));
        
for (let k in history) {
	let text = document.createElement("span");
	let node = document.createTextNode(history[k]);
	text.appendChild(node)
	para.appendChild(text);
	historyContainer.appendChild(para);
}
	
<div id="output">
	</div>

history.push(numbers.join(" ")); is your problem - by the time the loop runs history is an array with a single value of 1990 1991 1992 because of the join. In fact you don't even need history, just iterate over numbers

let numbers = [1990, 1991, 1992];
let historyContainer = document.getElementById("output");
let para = document.createElement("p");

for (let k in numbers) {
  let text = document.createElement("span");
  let node = document.createTextNode(numbers[k]);
  text.appendChild(node)
  para.appendChild(text);
  historyContainer.appendChild(para);
}
span+span {
  margin-left: 4px;
}
<div id="output"></div>

Append, Add Append Onto Your Vocabulary. Append is a somewhat formal word. Lawyers​, for example, often speak of appending items to other  verb (Formal) add, attach, join, hang, adjoin, fasten, annex, tag on, affix, tack on, subjoin His real name hadn't been appended to the manuscript. remove, separate, take off, detach, disconnect, disengage Collins Thesaurus of the English Language – Complete and Unabridged 2nd Edition. 2002 © HarperCollins Publishers 1995, 2002

If you want some simple and fast solution, try this:

var numbers = [1990, 1991, 1992],
  output = document.getElementById("output");

output.innerHTML = '<span>' + numbers.join('</span> <span>') + '</span>';
<div id="output"></div>

APPENDING, appending meaning: 1. present participle of append 2. to add something to the end of a piece of writing: . Learn more. SEE SYNONYMS FOR append ON THESAURUS.COM verb (used with object) to add as a supplement, accessory, or appendix; subjoin: to append a note to a letter. to attach or suspend as a pendant.

HTML:

<div id="output"></div>

JavaScript:

let numbers = [1990, 1991, 1992];

numbers.forEach(function(item) {
  var span = document.createElement("span");
  var text = document.createTextNode(item);
  span.appendChild(text);
  document.getElementById("output").appendChild(span);
});

CSS:

span {
  margin-right: 10px;
}

Appending, 1. to add as a supplement or appendix: to append a note to a letter. 2. to affix: to append one's signature to a will. [1640–50; < Latin appendere=ap  appending Appending such a row to a totally unimodular matrix preserves this property. Appending architecture to the visual arts sealed its derivative fate.

Try Following code :

let numbers = [1990, 1991, 1992];
let history = [];
let historyContainer = document.getElementById("output");
let para = document.createElement("p");
history.push(numbers.join(" "));
numbers.forEach(function(element){
  console.log(element);
    let text = document.createElement("span");
    let node = document.createTextNode(element);
  text.appendChild(node)
    para.appendChild(text);
  historyContainer.appendChild(para);
});

Expected HTML :

Append, to sign a document with; affix: to append one's signature to a will. QUIZZES. DO YOU KNOW THESE 9TH GRADE VOCABULARY WORDS? Test your vocab skills  verb (Formal) add, attach, join, hang, adjoin, fasten, annex, tag on, affix, tack on, subjoin His real name hadn't been appended to the manuscript. remove, separate, take off, detach, disconnect, disengage Collins Thesaurus of the English Language – Complete and Unabridged 2nd Edition. 2002 © HarperCollins Publishers 1995, 2002

append, To append means to add on, usually to the end of something. You might want to append a clause onto a contract if you feel something has been left unsaid in it. append definition: 1. to add something to the end of a piece of writing: 2. to add something to the end of a piece of…. Learn more.

append, append (third-person singular simple present appends, present participle appending, simple past and past participle appended). (transitive) To hang or attach to  Append: to join (something) to a mass, quantity, or number so as to bring about an overall increase. Synonyms: add, adjoin, annex… Antonyms: abate, bate, deduct

Append dictionary definition, To add as a supplement or appendix: appended a list of errors to the report. To fix to; attach: append a charm to the bracelet. Origin of append. Latin appendere to  In general, to append is to join or add on to the end of something. For example, an appendix is a section appended (added to the end) of a document. In computer programming, append is the name of a procedure for concatenating (linked) lists or arrays in some high-level programming languages.

Comments
  • history.push(numbers.join(" ")); this line is why you are getting a single span tag, .join will join all number into 1 value so history is basically an array containing just 1 element
  • Beat me to it! I would even go 1 step further to suggest some syntactic adjustments; using const instead of let for historyContainer, para. This isn't a huge deal, but since you are not intending to modify those variables, it's better suited that they be constant. If you wanted to be extra-safe, you should also check that historyContainer is actually populated with the expected DOM element.
  • @EvanBechtol agreed! Since it wasn't related to solving the problem I left it alone, but great call out.
  • @chazsolo Thanks a lot, this works very well. Funny that the solution is actually quite easy ... I probably got lost in too much code.