0

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>

Link to external jsfiddle

1
  • 1
    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 Commented Dec 3, 2018 at 20:12

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

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.
0

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>

Comments

0

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;
}

Comments

0

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 :

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.