/    Sign up×
Community /Pin to ProfileBookmark

Save Multiple Content Editable Inputs with localStorage

I have two content editable inputs at the top of the .workspaceMain class, one .mainHeader and the other .tableName.

I have created a save button at the top of the.workspaceMain class, within the .toolbar. I have also made an id of #edit within each of the .mainHeader and .tableName code sequences to work within the JavaScript code.

When I have one id of #edit within the .mainHeader code sequence and go to press the .saveContent button after changing the text, I refresh and the text is updated to the changed text.

If I have the id of #edit within .tableName however, when I go to change the text, save and refresh, the text of .tableName isn’t updated once saved.

How would I update my JavaScript code to accommodate for allowing multiple Content Editable inputs to be saved at once.

I have tried making the id’s unique using #edit1, #edit2 with changing the JavaScript to suit like so with no luck.

**JS**

“`
` function saveEdits() {
var editElem = document.getElementById(“edit1”);

var userVersion = editElem.innerHTML;

localStorage.userEdits = userVersion;
}

function checkEdits() {

if(localStorage.userEdits!=null)
document.getElementById(“edit2”).innerHTML=localStorage.userEdits;
}

function saveEdits() {
var editElem = document.getElementById(“edit1”);

var userVersion = editElem.innerHTML;

localStorage.userEdits = userVersion;
}

function checkEdits() {

if(localStorage.userEdits!=null)
document.getElementById(“edit2”).innerHTML=localStorage.userEdits;
}`
“`

**HTML**

“`
`<!DOCTYPE HTML>
<!–
~ Copyright (c) Summit Learning Management System (made by students, for students). 2019.
–>
<html lang=”en-AU”>
<head>
<title>Welcome &#8212; Summit &#8212; Kempsey Adventist School</title>
<link rel=”stylesheet” type=”text/css” href=”../style.css”> <!– Internal Stylesheet –>
<script src=”https://kit.fontawesome.com/d3afa470fb.js”></script> <!– Vector Icons –>
<link rel=”shortcut icon” href=”../Assets/Images/favicon.png”> <!– Favicon –>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, user-scalable=no, initial-scale=1.0″>
</head>
<body onload=”checkEdits()”>
<?php
session_start();

if (!isset($_SESSION[‘loggedin’]) || $_SESSION[‘loggedin’] == false ) {
header(“Location: index.php”);
}
?>
<div id=”wrapper”>
<div class=”navigation”>

<button class=”navLinks” title=”Logout”><i class=”fas fa-sign-out-alt”></i></button>
<button class=”navLinks” title=”Help”><i class=”fas fa-question-circle”></i></button>
<button class=”navLinks” title=”Quick Links”><i class=”fas fa-bookmark”></i></button>

<div class=”menuDropDown”>
<button class=”menuButton” title=”Site Navigation”><i class=”fas fa-bars”></i> Menu</button>
<div class=”menuContent”>
<div class=”menuRow”>

<div class=”menuColumn”> <!– Home Workspace –>
<h5 class=”menuHeader”>Home Workspace</h5>
<a href=”#” title=”Welcome” class=”current”><i class=”fas fa-door-open”></i> Welcome</a>
</div>

<div class=”menuColumn”> <!– Learning Workspace –>
</div>

<div class=”menuColumn”> <!– Student Management Workspace –>
</div>

<div class=”menuColumn”> <!– Administration Workspace –>
</div>

</div>
</div>
</div>
</div>
<div class=”workspaceMain”>
<div class=”toolbar”>
<button class=”saveContent” onclick=”saveEdits()” title=”Save Changes”><i class=”fas fa-save”></i> Save</button>
<button class=”toolLinks” title=”Collapse Panel”><i class=”fas fa-arrow-right”></i></button>
<button class=”toolLinks” title=”Change Background Colour”><i class=”fas fa-fill-drip”></i></button>
</div>

<h3 class=”mainHeader” id=”edit1″ contenteditable=”true”>KEMPSEY ADVENTIST SCHOOL</h3>
<table class=”tableSet”>
<caption class=”tableName” contenteditable=”true”>Weekly Outline</caption>
</table>
</div>

<div class=”workspaceSide”></div>
</div>
</body>
</html>`
“`

to post a comment
HTMLJavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 11.2019 — I have tried making the id's unique using #edit1, #edit2 with changing the JavaScript to suit like so with no luck.[/quote]This should work and might be appropriate if there are only two elements to save. Please show your attempt so that we can fix it.

When there would be more elements it would be advisable to use a class and a loop instead.
Copy linkTweet thisAlerts:
@tcarpenterauthorJul 11.2019 — @Sempervivum#1606182 how would i go about this in my JS Code.
Copy linkTweet thisAlerts:
@SempervivumJul 11.2019 — how would i go about this in my JS Code.[/quote]
Do you mean this procedure:
use a class and a loop instead.[/quote]?
Copy linkTweet thisAlerts:
@daveyerwinJul 11.2019 — maybe just 'wrap' the content editable elements ...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en-AU"&gt;
&lt;head&gt;
&lt;title&gt;Welcome &amp;#8212; Summit &amp;#8212; Kempsey Adventist School&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="../style.css"&gt; &lt;!-- Internal Stylesheet --&gt;
&lt;script src="https://kit.fontawesome.com/d3afa470fb.js"&gt;&lt;/script&gt;&lt;!-- Vector Icons --&gt;
&lt;link rel="shortcut icon" href="../Assets/Images/favicon.png"&gt; &lt;!-- Favicon --&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="toolbar"&gt;
&lt;button class="saveContent" onclick="saveEdits()" title="Save Changes"&gt;&lt;i class="fas fa-save"&gt;&lt;/i&gt; Save&lt;/button&gt;
&lt;/div&gt;
&lt;div id=editableStuff&gt;
&lt;h3 class="mainHeader" contenteditable="true"&gt;KEMPSEY ADVENTIST SCHOOL&lt;/h3&gt;
&lt;table class="tableSet"&gt;
&lt;caption class="tableName" contenteditable="true"&gt;Weekly Outline&lt;/caption&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;script&gt;
var editElem = document.getElementById("editableStuff");
function saveEdits() {
localStorage.userEdits = editElem.innerHTML; }
function checkEdits() {
if(localStorage.userEdits !=null )
editElem.innerHTML=localStorage.userEdits; }
checkEdits();
&lt;/script&gt;
&lt;/body&gt;<i>
</i>
``

</html>
Copy linkTweet thisAlerts:
@tcarpenterauthorJul 12.2019 — @Sempervivum#1606189 Yes how would it look like in my existing JS Code.
Copy linkTweet thisAlerts:
@SempervivumJul 12.2019 — Javascript:
var elems = document.querySelectorAll('.to-save');
function saveEdits() {
for (var i = 0; i &lt; elems.length; i++) {
var content = elems[i].innerHTML;
localStorage.setItem('content' + i, content);
}
}
function checkEdits() {
for (var i = 0; i &lt; elems.length; i++) {
var content = localStorage.getItem('content' + i);
if (content) elems[i].innerHTML = content;
}
}

Apply the class "to-save" to every element that needs to be saved, like this:
&lt;h3 class="mainHeader" id="edit1" class="to-save" contenteditable="true"&gt;KEMPSEY ADVENTIST SCHOOL&lt;/h3&gt;
×

Success!

Help @tcarpenter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...