Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
retrieving and rendering messages from sessionStorage from a form

In a course I'm taking my lecturer wrote the below code for submitting data from a form when the submit button is pressed. It stores this to sessionStorage and then renders the received messages below the form. I made sure to copy it exactly and mine doesn't work. It seems to be because the .fullName, .email, .feedbackType and .comment are not being assigned a type. When I ran the lecturers code file from my vscode and checked it in the browser his worked fine. My work is identical to his bar some different CSS styles. It is currently just putting undefined for each item

function submitMessage(event) {
  event.preventDefault();
  const email = document.getElementById('email').value;
  const fullName = document.getElementById('fullName').value;
  const feedbackType = document.getElementById('feedbackType').value;
  const comment = document.getElementById('comment').value;
  const messageObject = {
    email,
    fullName,
    feedbackType,
    comment,
  };

  let currentMessages = [];

  if (window.sessionStorage.getItem('messages')) {
    currentMessages = JSON.parse(
      window.sessionStorage.getItem('messages')
    );
  }

  currentMessages.push({
    messageObject
  });
  /*temporary fix, find a solution for this error, .push not working/recognised. Code matching lectures is currentMessages.push(messageObject)*/

  window.sessionStorage.setItem(
    'messages',
    JSON.stringify(currentMessages)
  );
  renderMessages();
}

function renderMessages() {
  let currentMessages = [];

  if (window.sessionStorage.getItem("messages")) {
    currentMessages =
      JSON.parse(
        window.sessionStorage.getItem("messages")
      );
  }

  let listItems = [];
  for (let i = 0; i < currentMessages.length; i++) {
    let listItem = " ";
    const currentMessage = currentMessages[i];
    listItem += `<dt>${currentMessage.fullName} - ${currentMessage.email}</dt>`;
    listItem += `<dd>${currentMessage.feedbackType}: ${currentMessage.comment}</dd>`;
    listItem += `<br />`;
    listItems.push(listItem);
  }
  let descList = document.getElementById('currentMessages');
  descList.innerHTML = listItems.join('');
}

renderMessages();

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!