Why Does This Work In Jsfiddle But Not In My Document
I found a wonderful jsfiddle that someone has made and wanted to use part of it in my project: http://jsfiddle.net/manuel/29gtu/ It works on the jsfiddle but not in my HTML documen
Solution 1:
You should wrap your whole code within $(document).ready(function() {...});
So.
<scripttype="text/javascript">
$(document).ready(function() {
$("button").click(function() {
var id = $("#id").val();
var text = "icon-" + id;
// update the result arrayvar result = JSON.parse(localStorage.getItem("result"));
if (result == null) result = [];
result.push({
id: id,
icon: text
});
// save the new result arraylocalStorage.setItem("result", JSON.stringify(result));
// append the new li
$("#bxs").append($("<li></li>").attr("id", "item-" + id).html(text));
});
// on init fill the ulvar result = JSON.parse(localStorage.getItem("result"));
if (result != null) {
for (var i = 0; i < result.length; i++) {
var item = result[i];
$("#bxs").append($("<li></li>").attr("id", "item-" + item.id).html(item.icon));
}
}
});
</script>
Note
In jsfiddle
onLoad
mode do that for you, i.e. when you select onLoad
from left side panel drop down, then all js code execute after all resources become appeared in the DOM.
Solution 2:
Put in $(document).ready
like this, Also give type of script tag
<scripttype="text/javascript">
$(document).ready(function() {
$("button").click(function() {
var id = $("#id").val();
var text = "icon-" + id;
// update the result arrayvar result = JSON.parse(localStorage.getItem("result"));
if (result == null) result = [];
result.push({
id: id,
icon: text
});
// save the new result arraylocalStorage.setItem("result", JSON.stringify(result));
// append the new li
$("#bxs").append($("<li></li>").attr("id", "item-" + id).html(text));
});
// on init fill the ulvar result = JSON.parse(localStorage.getItem("result"));
if (result != null) {
for (var i = 0; i < result.length; i++) {
var item = result[i];
$("#bxs").append($("<li></li>").attr("id", "item-" + item.id).html(item.icon));
}
}
});
</script>
Post a Comment for "Why Does This Work In Jsfiddle But Not In My Document"