I'm trying to update the DOM with my localStorage values, but I can't figure out what's wrong. I'm guessing the problem has to do with the page refreshing or variable scopes?
It's basically just a simple ticket form, which should open a filled ticket when submitted.
Ticket Form Here we enter our input
<form action="" class="ticket-form">
<label for="subject">Subject</label>
<input type="text" id="subject" name="ticket-subject" required />
<label for="description">Description</label>
<input type="text" id="description" name="ticket-description" required>
<label for="priority">Priority</label>
<select id="priority" name="ticket-priority">
<option value="minor" selected>1 - Minor</option>
<option value="significant">2 - Significant</option>
<option value="critical">3 - Critical</option>
</select>
<a>
<form action="./ticket.html">
<input type="submit" value="Submit" id="ticket_submit">
</form>
</a>
</form>
Event listener for submit button. Set input to localStorage
ticketForm.addEventListener('submit', (e)=> {
const ticketSubject = document.getElementById('subject').value;
const ticketDescription = document.getElementById('description').value;
const prio = document.getElementById('priority')
const priorityValue = prio.options[prio.selectedIndex].value
e.preventDefault()
localStorage.setItem('subject', JSON.stringify(ticketSubject));
localStorage.setItem('description', JSON.stringify(ticketDescription));
localStorage.setItem('priority', JSON.stringify(priorityValue));
getTicketFormValues()
});
The HTML I need to update
<div class="container">
<div class="canvas">
<h1 class="ticket-subject">Lorem ipsum dolor</h1>
<h2>Description</h2>
<p class="ticket-description">Lorem ipsum dolor sit.</p>
<h2>Urgency</h2>
<p class="ticket-priority">Lorem, ipsum.</p>
</div>
</div>
All is seemingly going well thus far, as the localStorage does have the correct key-value pairs. It is only when I try to manipulate the HTML with this information something goes wrong.
function getTicketFormValues() and DOM element variables
const ticketForm = document.querySelector('.ticket-form')
const subjectElTicket = document.querySelector('.ticket-subject')
const descriptionElTicket = document.querySelector('.ticket-description')
const priorityElTicket = document.querySelector('.ticket-priority')
function getTicketFormValues() {
const subjectValue = localStorage.getItem('subject')
const descriptionValue = localStorage.getItem('description')
const urgencyValue = localStorage.getItem('priority')
subjectElTicket.innerHTML = subjectValue;
descriptionElTicket.innerHTML = descriptionValue;
priorityElTicket.innerHTML = urgencyValue;
location.href='ticket.html'
};