I have a very annoying problem. I have a button called #log-out
and when you click it it saves a certain peice of data in a localStorage variable called importantOSdata
.
When the page loads it checks if the variable != null
and if it isn't removes the div that was saved in the localstorage variable from the page and replaces it with the one that IS saved in the localStorage variable. The problem is as the title says, jquery events will not work on the dynamic divs even though they already worked when there was no saved data and it was just plain hardcoded html. I have tried using .on() but as you can see in the code I use the this
keyword and will not work properly. I MUST use the this
keyword.
(1)The code I have has a click event for the div and checks if it has a certain type of data- tag (data-title) and a plain old src tag. This is then passed into a function which creates a popup iframe with src from the src tag. (1)The code that used to work:
$(".icon").each(function() {
var iconFile = $(this).attr("data-iconFile");
var title = $(this).attr("data-title");
var file = $(this).attr("src");
$(this).css("backgroundImage", "url(" + iconFile + ")");
$(this).dblclick(function() {
new createFrame(file,title);
addItem($(this).attr("class"),iconFile);
//canSlideUp = false;
});
});
The new code I've tried:
$(".icon").each(function() {
var iconFile = $(this).attr("data-iconFile");
var title = $(this).attr("data-title");
var file = $(this).attr("src");
$(this).css("backgroundImage", "url(" + iconFile + ")");
$(this).on("dblclick",function() {
new createFrame(file,title);
addItem($(this).attr("class"),iconFile);
//canSlideUp = false;
});
});
});
And the save/load code:
$(document).ready(function() {
$("#log-out").click(function() {
alert("Your Progress was saved:"+localStorage.getItem("inportantOSdata"));
localStorage.setItem("inportantOSdata",document.getElementById("data-holder").innerHTML);
});
});
window.onload = function (){
if(localStorage.getItem("inportantOSdata") != null) {
$("#data").remove();
alert("data exists");
$("#data-holder").html(localStorage.getItem("inportantOSdata"));
}
}
//localStorage.clear();
You can see how I use the this keyword. ANy ides an this interesting problem? Thanks in advanced!