I suspect this is what you wanted to achieve :
<div id="wrapper">
<div id="editable" contenteditable="true"></div>
<button onclick="localStorage.clear()">Reset</button>
<br>
<textarea id="message"></textarea>
</div>
<script>
function init()
{
function setup (name, prop)
{
function save (prop)
{
localStorage.setItem (this.id, this[prop]);
}
var elem = document.getElementById(name);
// retrieve value
elem[prop] = localStorage.getItem (name) || '';
// setup save handler
//elem.onkeyup = save.bind (elem, prop);
elem.onkeyup = function (e,p) { // IE8+ compat.
return function () {
save.call (e, p);
};
}(elem, prop);
}
setup ('editable', 'innerHTML');
setup ('message' , 'value');
}
window.onload = init;
</script>
Your code was flawed in so many ways I reckoned it was easier to rewrite it from scratch:
- complete duplication of code for the saving/restoring of your 2 elements, with the code located in two different places while the problem is basically the same
- confusing names ('ths' is an eyesore. The first time I checked your code I automatically identified it as a typo for 'this')
- wrong way of defining event handlers and passing them parameters (defining event handlers inside HTML code is causing all sorts of problems, since you can't access anything but
this
and global variables)
- mumble-jumble of global and local variables (due to the definition of the event handlers inside HTML)
- your code did not work in the fiddle since all your global functions were moved into the init procedure
It was much less work (at least for me) to rewrite it than to try to rebuild a functional version and then try to understand what went wrong with it.
I dumped the attempt at detecting whatever IE7 version. It was getting in the way, since your problem was targeting IE10 anyway. As a side note, a site using this kind of features should simply drop IE7- compatibility altogether, IMHO.
I tested this code on IE8/XP, FF, Opera, Chrome, IE11 and safari/XP.
All tests were run from a web server except IE11. It is well possible IE10- have problems with local storage when run localy.