Storing data locally with Localstorage and manipulating it with Jquery

I'm experiencing an issue with localstorage that I need help with.

Context I am currently working on a web application for quotes that requires a bookmark system to save users' favorite quotes. To address this issue, I have created a jQuery script that dynamically generates a div through an "onclick" function (as shown below):

function createLink1() {
  jQuery('<div class="item"><a>Dynamically created element 1</a><button class="delete-button"></button></div>').find('a').attr('href', '#').end().appendTo('#bookmarks');
}

function createLink2() {
  jQuery('<div class="item"><a>Dynamically created element 2</a><button class="delete-button"></button></div>').find('a').attr('href', '#').end().appendTo('#bookmarks');
}

function createLink3() {
  jQuery('<div class="item"><a>Dynamically created element 3</a><button class="delete-button"></button></div>').find('a').attr('href', '#').end().appendTo('#bookmarks');
}

function createLink4() {
  jQuery('<div class="item"><a>Dynamically created element 4</a><button class="delete-button"></button></div>').find('a').attr('href', '#').end().appendTo('#bookmarks');
}



$(document).on('click', '.delete-button', function() {
  $(this).parent().remove();
});
#bookmarks {
  width: 100%;
  height: auto;
  background: rgba(236, 233, 233, 1.00);
  margin-bottom: 30px;
}
.item {
  height: 30px;
  font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
  font-size: 16px;
  margin-bottom: 20px;
  background: rgba(229, 226, 140, 1.00);
}
.delete-button {
  width: 28px;
  height: 28px;
  background: rgba(222, 28, 31, 1.00);
  right: 5px;
}
.quote {
  width: 100%;
}
.title {
  font-size: 24px;
  font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
  text-align: center;
}
.paragraph {
  font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
  font-size: 16px;
  text-align: center;
}
.button {
  display: block;
  width: 70px;
  height: 50px;
  background: rgba(77, 76, 184, 1.00);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Bookmarks</title>


</head>

<body>

  <div id="bookmarks"></div>

  <div class="quote">
    <h1 class="title">Quote Title</h1>
    <p class="paragraph">kjfkl jgdfsklhsdfjkbsjl hauñ g hgauñigh erajkh uh ad.jk hñhakjh gulhrj sjhg slkghdsjkh slh gajk gh iñhsjk jldfsh grñkj s hñihg sjdh</p>
    <button class="button" onclick="createLink1();"></button>
  </div>

  <div class="quote">
    <h1 class="title">Quote Title</h1>
    <p class="paragraph">kjfkl jgdfsklhsdfjkbsjl hauñ g hgauñigh erajkh uh ad.jk hñhakjh gulhrj sjhg slkghdsjkh slh gajk gh iñhsjk jldfsh grñkj s hñihg sjdh</p>
    <button class="button" onclick="createLink2();"></button>
  </div>

  <div class="quote">
    <h1 class="title">Quote Title</h1>
    <p class="paragraph">kjfkl jgdfsklhsdfjkbsjl hauñ g hgauñigh erajkh uh ad.jk hñhakjh gulhrj sjhg slkghdsjkh slh gajk gh iñhsjk jldfsh grñkj s hñihg sjdh</p>
    <button class="button" onclick="createLink3();"></button>
  </div>

  <div class="quote">
    <h1 class="title">Quote Title</h1>
    <p class="paragraph">kjfkl jgdfsklhsdfjkbsjl hauñ g hgauñigh erajkh uh ad.jk hñhakjh gulhrj sjhg slkghdsjkh slh gajk gh iñhsjk jldfsh grñkj s hñihg sjdh</p>
    <button class="button" onclick="createLink4();"></button>
  </div>

</body>

</html>

Issue Unfortunately, the current code doesn't save when the page is closed or refreshed. To rectify this, I plan to utilize "localstorage" to save the generated content, but I'm struggling to implement it into the JavaScript to create bookmark pages within my web application. Any suggestions or solutions would be greatly appreciated! Thank you!

Answer №1

Keeping data in the local storage is a simple task.

To store a JSON or an array:

localStorage.setItem("quotes", JSON.stringify({
 quote1: "This is a quote."
}));

Retrieving:

var quotes = JSON.parse(localStorage.getItem("quotes"));

Why should I use stringify and parse? Because localStorage only accepts strings, integers, and booleans, not JSON.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

invoking a function in javascript from react

Currently, I am grappling with the task of invoking a JavaScript function from within React while adapting a "React" based "Menu" onto some existing jQuery functions. Below you can find my code for the menu: class Menus extends React.Component{ co ...

Ways to eliminate submenu tooltips

Whenever I hover over a menu item with submenu pages in the wordpress backend, a "tooltip" pops up showing each submenu page. How can I remove these tooltips? I have attempted to remove the wp-has-submenu style class, which somewhat works. The tooltip no ...

Have the quotes within my markup been replaced with HTML entities?

Currently, I am working on a content page that uses a master page which includes a text box and a button. My goal is to have the button execute some JavaScript code before performing any other actions. At the moment, I am in the testing phase of this JavaS ...

Trigger the onClick event of an element only if it was not clicked on specific child elements

<div onClick={()=>do_stuff()}> <div classname="div-1"></div> <div classname="div-2"></div> <div classname="div-3"></div> <div classname="div-4"></div> ...

Unable to load the requested resource: net::ERR_FAILED

I am facing an issue while trying to write React code. When using Chrome, I encountered the following warning: Access to XMLHttpRequest at 'file:///D:/programe/code/Vscode/ReactDemo/HelloWorld/test.js' from origin 'null' has been block ...

Revisiting the Issue: Jquery Hover Effect Fails to Function in Internet Explorer

Can anyone provide assistance with this issue? The code snippet works perfectly in all browsers except for IE, where the menu does not fade in and looks unattractive. html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

Angular: Utilizing Nested ng-repeat Alongside groupBy Feature on Initial Page Load or Refresh

With some help, I've made it this far on my project. However, I need to take one more step to achieve my goal. I want to group data based on an attribute that is currently passed through an ng-click action. Is there a way to automatically do this on p ...

Maximizing efficiency in JavaScript by utilizing jQuery function chaining with deferred, the .done() function

I am working on retrieving data from multiple functions and want to chain them together so that the final function is only executed when all the data has been successfully loaded. My issue arises when trying to use the .done() method, as it calls the func ...

This error is thrown when trying to access the property 'message' of an undefined value

I'm currently working on adding an AJAX contact form to my website. However, I've run into a problem where when I try to submit the form, I encounter the following error in Google Chrome: "Uncaught TypeError: Cannot read property 'message&a ...

Ways to center a spinner on the screen using CSS during loading

Upon loading the page, my spinner appears in the center of the screen after a second. However, in the initial frame of the page, it is not centered but positioned on the top-left corner instead. How can I ensure that my spinner starts off centered from the ...

Guide to utilizing JavaScript for a basic gaming experience

Looking to incorporate multiple divs that will vanish upon being clicked, while also increasing the score by 1 through javascript. Any suggestions on how to accomplish this? ...

Is it possible to enable an email hyperlink to function on a computer?

I successfully created an email link that is functional on mobile devices by using 'href="mailto:[email protected]"'. When I click on the button from a mobile device, it prompts me to choose how I want the email delivered. However, when I tr ...

Displaying images on the left side using CSS is causing issues in an ASP.NET project

Having trouble displaying a girl image in asp.net. I'm aiming for the following result: The problem is that the girl's face is not showing up correctly as shown below: This is the CSS I have created: .testimonialstilabel { background-image ...

Django is refusing to display my CSS and JavaScript files - static files

Within my code in settings.py, I define the static URL like so: STATIC_ROOT = os.path.join(BASE_DIR, "static") STATIC_URL = '/static/' The location of my static files and folders is at /home/myLinuxUsername/myApp/static/interactive-preview-depe ...

Is it possible to delay the execution of the code until the ajax function has completed

After executing the fillContent function, I need to call beginEditingQuestion. fillContent(cid, "questions"); beginEditingQuestion(qid); The issue is that beginEditingQuestion can't be executed until all the ajax requests in fillContent are complete ...

Determine if the same origin policy is in effect

Is there a method to determine if the same origin policy is applicable to a URL before attempting to use ajax methods? Below is an example of what I currently have: function testSameOrigin(url) { var loc = window.location, a = document.create ...

Utilizing jQuery to Uppercase the Ajax Attribute

Lately, I've encountered an issue where I need to format the text input before sending it to ajax and saving it in the database. Specifically, I want to convert the text to lowercase and then capitalize the first letter before storing it. Here's ...

Tips for displaying a div near the cursor's location when hovering in React JS

Looking for a way to show a hidden div at cursor position when hovering on Text item1 or item2. Check out the sample GIF animation in this Link My attempt using Jquery code inside React resulted in an error: $(".text-item").mouseenter(function ( ...

Can you tell me the proper term for the element that allows CSS properties to be changed after a link has been clicked?

I have integrated a horizontal scrolling date selector on my website, and it is functioning well. However, I am facing an issue while trying to change the CSS properties of a clicked date link using jQuery. Despite successfully firing the click event, I am ...

Maximizing jQuery DataTables performance with single column filtering options and a vast amount of data rows

My current project involves a table with unique column select drop-downs provided by an amazing jQuery plug-in. The performance is excellent with about 1000 rows. However, the client just informed me that the data could increase to 40000 rows within a mont ...