javascript - Saving some contenteditables localstorage - - - -
i'm trying save more 1 entry of contenteditable content localstorage chrome extension. current code saves 1 contenteditable section fine, when try add id of seperate contenteditable section either deletes saved information or doesn't @ all. i'm pretty novice in js, hope i'm making simple mistake. html looks this:
<div id = "content"> <div id= "tcontent" contenteditable="true" data-ph=" make note . . . " style= "height: 300px; overflow: auto"></div> <div id = "content2"> <div id= "tcontent2" contenteditable="true" data-ph= " make note . . . " style= "height: 300px; overflow: auto"></div> </div>
and javascript:
window.addeventlistener('load', onload); function onload() { checkedits(); } function checkedits() { if(localstorage.useredits!=null) { document.getelementbyid("tcontent", "tcontent2").innerhtml += localstorage.useredits; } }; document.onkeyup = function (e) { e = e || window.event; console.log(e.keycode); saveedits(); }; function saveedits() { var editelem = document.getelementbyid("tcontent", "tcontent2"); var userversion = editelem.innerhtml; localstorage.useredits = userversion; };
basically code save 1 (the content place first getelementbyid). isn't there way save both of 'content's? i've been playing around little knowledge of javascript have can't seem see i'm doing wrong or should doing here.
much , help.
document.getelementbyid method takes 1 element's id. trying pass 2 strings method. not work.
please refer documentation here: https://developer.mozilla.org/en-us/docs/web/api/document/getelementbyid
also, must assign innerhtml of each element individually each piece of saved content in localstorage.
granted new language not want overcomplicate answer you. said, please find below code few modifications able save both pieces in localstorage respectively:
window.addeventlistener('load', onload); function onload() { checkedits(); } function checkedits() { if(localstorage.useredits1!=null) { document.getelementbyid("tcontent").innerhtml = localstorage.useredits1; } if(localstorage.useredits2!=null) { document.getelementbyid("tcontent2").innerhtml = localstorage.useredits2; } }; document.onkeyup = function (e) { e = e || window.event; console.log(e.keycode); saveedits(); }; function saveedits() { var editelem1 = document.getelementbyid("tcontent"); var editelem2 = document.getelementbyid("tcontent2"); localstorage.useredits1 = editelem1.innerhtml; localstorage.useredits2 = editelem2.innerhtml; };
Comments
Post a Comment