Looking for a solution for saving and restoring user comments on dynamically created form fields in a page where users can comment on posts. How can we save and restore the typed comments before an ajax refreshes the div holding all the post and comments?
Attempted to implement the following code without success:
<textarea id="comment_field" onKeyUp="return saveAndRestoreTypedStrings(this)"></textarea>
or
<textarea id="comment_field" onchange="saveAndRestoreTypedStrings(' + id + ');"></textarea>
The function used is as below:
function saveAndRestoreTypedStrings(id){
document.getElementById("post_comment").onchange = function() {
localStorage['post_comment'] = document.getElementById(id).value;
}
window.onload= function(){
if(localStorage['post_comment'])
document.getElementById(id).value = localStorage['post_comment'];
}
}
In order to distinguish the form a user is working on, a unique id has to be passed to the saveAndRestoreTypedStrings(id) function.
This issue occurs when ajax refreshes the div containing the posts and comments every 3 seconds. If a user is typing a comment and the div reloads, the typed content is lost. Would appreciate any suggestions or ideas on how to tackle this problem.