JQuery sets local storage variable

I'm having trouble getting a local storage variable to store the correct value. Its essence is that I want to display the contents of a local variable, and then, if the user clicks, it extracts data from the XML file, saving it in a local variable.

The problem is that it does not save the correct local variable. I tried the syntax to make it work, and I'm not in ideas.

The site for testing is located at http://web.engr.oregonstate.edu/~todtm/assignment2.html

Script Code:

function startAjax() { $("#clickme").text("Calling server"); $.ajax( { url: "xmlpage.xml", success: callbackFunction, error: errorFunction }); } function callbackFunction(data, info) { var titles = $(data).find("title"); if (titles && titles.length) { $("#results").text("result:" + titles.text()); localStorage.setItem('titles', #results.text()); } else errorFunction(data, "No titles"); } function errorFunction(data, info) { $("#clickme").text("error occurred:" + info); } $(document).ready(function () { $("#results").text(localStorage.getItem('titles')); }); 
+4
source share
1 answer

you have a syntax error, you need to get

 localStorage.setItem('titles', $('#results').text()); 

or

 localStorage.setItem('titles', titles.text()); 
+14
source

Source: https://habr.com/ru/post/1492418/


All Articles