I'm using jQuerymobile, and I have a listview. In the list view I have dynamic content that is loaded from localStorage. When you click a specific listitem, I want the user to go to the next page, which the associated values from that specific localstorage object.
I can kind of get my code to work below (which I've commented). The problem is,
var favoritedName = JSON.parse( localStorage.getItem(placesText));
returns NULL.
But if I manually load in
var favoritedName = JSON.parse( localStorage.getItem("placesFireside Bar"));
it works fine.
I'm wondering if you can dynamically load in .text() from a listitem you click in a listview, and then set that as the key in localstorage.getItem().
Here's my code:
// click the list item, which is a specific bar
$('#all-my-favorites li').click(function() {
// get specific name of Bar from the list item
listitemText = $(this).text();
// OR manually name the list item. example here is "Fireside Bar" for testing purposes
//listitemText = "Fireside Bar";
// combine the strings, so i can search localstorage for the relevant key item
placesText = "places" + listitemText;
//alert(placesText);
// returns "placesFireside Bar" or "places(barname from listitem)"
});
// go to the new page, which should show content from the relevant key item
$('#favorited').on('pagebeforeshow', function() {
// test to see if placesText is a string
alert(jQuery.type( placesText ) === "string");
// returns TRUE
// find the relevant keyitem from localstorage, and name the object favoritedName
var favoritedName = JSON.parse( localStorage.getItem(placesText));
alert(favoritedName);
// returns NULL
$(this).find('[data-role=header] .ui-title').text(favoritedName.name);
$('#FavoritedplaceName').text(favoritedName.name);
$('#FavoritedlocationName').text(favoritedName.location);
$('#FavoritedtimeofDay').text(favoritedName.timeOfDay);
$('#FavoriteddayofWeek').text(favoritedName.dayofWeek);
});