0

I'm making a simple page which displays 7 days on button click. I'd like the days to be displayed in a vertical list type of format, but the days are being displayed in a single line. When I try using the forEach function to display 'dayslist', it only shows one day name. How can it be displayed in a vertical list?? Thankyou.

enter image description here

HTML:

<div id="weekdivid">
  <h3>Days in a week</h3>
  <button onclick="showdays()">Show days</button>
  <p id="showdayspid"></p>
</div>

Javascript:

function showdays() {
  const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];
  days.forEach(function (daylist) {
    document.getElementById("showdayspid").innerHTML = days + "<br>";
  });
}
4
  • 1
    You have JavaScript in your HTML: snippet. Commented Jun 23, 2021 at 15:27
  • Why aren't you using daylist in your forEach loop? Commented Jun 23, 2021 at 15:29
  • 3
    You're using the wrong variable, and you're not appending: .innerHTML += daylist + "<br>" Commented Jun 23, 2021 at 15:30
  • 1
    Shouldn't document.getElementById("showdayspid").innerHTML = days + "<br>"; be document.getElementById("showdayspid").innerHTML += daylist + "<br>";, instead? Commented Jun 23, 2021 at 15:30

4 Answers 4

2

You can use array#join() to add br tag.

(function showdays() {
  const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];
  document.getElementById("showdayspid").innerHTML = days.join('<br>');
})();
<div id='showdayspid'></div>

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

Comments

1

You need to use += instead of = in your foreach, something like:

function showdays() {
  const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];
  days.forEach(function (day) {
    document.getElementById("showdayspid").innerHTML += day + "<br>";
  });
}

Comments

1

You could also refactor this slightly so that you're not using getElementById unnecessarily in the forEach to find the same element each time.

function showdays() {
    const dayNode = document.getElementById("showdayspid");
    const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];
    let dayResult;
    days.forEach(function (day) {
      dayResult += day + "<br>";
    });
   dayNode.innerHTML = dayResult;
}

Comments

1

As you are displaying a list of related items, it would be advisable to use the proper HTML tag (either <ul></ul> or <ol></ol>):

const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];

function populateDaysList() {
  // select the list element
  const listElement = document.querySelector('#daysList');
  
  // if needed, you can style it to make it look like 'normal' text
  // or, even better, you style it in the CSS
  // listElement.style.listStyle = 'none';
  // listElement.style.padding = 0;
  
  days.forEach(item => {
    // create the list item...
    const li = document.createElement('li');
    // ... add the text to it...
    li.innerText = item;
    // ... and append it to the list element
    listElement.append(li);
  })
}

populateDaysList()
#daysList {
  list-style: none;
  padding: 0
}
<ul id='daysList'>

</ul>

Anyway, if for any reason you have to stick to the approach you used, you have to amend your code so that it adds new lines to the parent element innerHTML rather than entirely replacing it with a stringified version of the array followed by <br>:

const days = ["Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"];

function showdays() {
  const containerElement = document.getElementById("showdayspid");
  days.forEach(day => containerElement.innerHTML += `${day}<br>`)
}

showdays();
<div id=showdayspid>

</div>

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.