I am working on a project that would allow the client to save a link to the webpage without having to do any backend coding. I have html and js written to allow this, but the js resets when you leave the page and the link goes away. How could I make these links permanent after addition even if the page is reloaded, etc.? FYI: I am a novice at coding, so dumbing down answers for me is appreciated.
Here is my html and js code:
<form action="#" id="createElForm">
<label for="" style="display: block;">Write or paste a link</label>
<input type="text" placeholder="www.example.com" id="linkInput" required>
<label for="" style="display: block;">Text to display</label>
<input type="text" placeholder="Example Text" id="textInput" required>
<br>
<br>
<button type="submit" style="display: block;">Submit</button>
</form>
const form = document.querySelector("#createElForm");
const table = document.querySelector("table");
form.addEventListener("submit", function (e) {
e.preventDefault();
const linkValue = document.querySelector("#linkInput").value;
const textValue = document.querySelector("#textInput").value;
const newRow = document.createElement("tr");
const newCell = document.createElement("td");
const newLink = document.createElement("a");
newLink.href = linkValue;
newLink.target = "_blank";
newLink.style.color = "blue";
newLink.textContent = textValue;
newCell.appendChild(newLink);
newRow.appendChild(newCell);
table.appendChild(newRow);
form.reset();
});
2
Answers
You can save and retrieve data using localstorage
So, to save data
and to recieve data
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
(excuse my english and speling)
create a javascript object and pt that information in it,
then save the object to your lacal storage, then upon page reload get the object and create the link
this function creates the links and returns atable row (tr element) with the links
if there is anything i need to clarify or if i misunderstood what you wanted to do please comment,