Search code examples
splitprompt

using string split method to split prompt values


I am trying to split my prompt values so that each word appears on a different line, but my code makes it so that the words are split by commas instead of to each line like I want it to. Thank you so much for helping!

<p div id="a"></div></p>

<script>

window.onload=function() {
var x=prompt("Enter Terms", "Terms")
document.getElementById("a").innerHTML=x.split(" ");
      for(var i=0; i < a.length; i++){
document.write(a[i]);
}

</script>

Solution

  • Use Array.prototype.join(), joins all elements of an array into a string using specified argument

    window.onload = function() {
      var x = prompt("Enter Terms", "Terms");
      document.getElementById("a").innerHTML = x.split(" ").join('<br/>');
    }
    <div id="a"></div>