0

What I'm trying to do is take a comma separated string like "HTML5,CSS3,Compass,JavaScript,jQuery,PHP,Foundation,Drupal,WordPress" - split that string into and array and write a loop that outputs each array item on a separate line.

This works:

function splitstr() {
 var splitStr = "HTML5,CSS3,Compass,JavaScript,jQuery,PHP,Foundation,Drupal,WordPress";
 var output = splitStr.split(',');
 for (var i = 0; i < output.length; i++) {
   document.write(output[i] + "<br />");
 }
}

but obviously it outputs to a blank page. I need it to output the results into a div.

So I try the following code but it doesn't work. It only prints out the last one "Wordpress".

function splitstr() {
 var splitStr = "HTML5,CSS3,Compass,JavaScript,jQuery,PHP,Foundation,Drupal,WordPress";
 var output = splitStr.split(",");
 for (var i = 0; i < output.length; i++) {
  document.getElementById("splitres").innerHTML = output[i] + "<br />";
 }
}

what am i doing wrong?

agon

1 Answer 1

4
 document.getElementById("splitres").innerHTML += output[i] + "<br />";

Note +. With that, you are appending HTML; without it, you are overwriting it.

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

2 Comments

Nice & concise explanation! +
@agon024, please accept the answer if it's the correct one.

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.