2

How can I save contenteditable element with javascript(no PHP) into actual HTML code? So I can edit content whenever even in offline mode. Like when you click "save button" it replace old file with new one(text with changes).

If there is a way to make this work in offline mode with any other programming lang please suggest.

I found a few examples but they were all made with PHP.

Also, I will post code. In this code, you are able to edit the file with javascript and save it. But problem is that it does not save into actual HTML code.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<style type="text/css">
 body{ 
  font-family: "Dosis"; 
  font-size: 1.3em;
  line-height: 1.6em;
}

.headline{
  font-size: 2em;
  text-align: center;
}

#wrapper {
  width: 600px;
  background: #FFF;
  padding: 1em;
  margin: 1em auto;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  border-radius: 3px;
}

button {
  border: none;
  padding: 0.8em;
  background: #F96;
  border-radius: 3px;
  color: white;
  font-weight: bold;
  margin: 0 0 1em;
}

button:hover, button:focus {
  cursor: pointer;
  outline: none;
}

#editor {
  padding: 1em;
  background: #E6E6E6;
  border-radius: 3px;
}

</style>
<body>

<div id="wrapper">

  <section>
    <h1 class="headline">contentEditable Demonstration</h1>

    <button id="editBtn" type="button">Edit Document</button>
    <div id="editDocument">
      <h1 id="title">A Nice Heading.</h1>
      <p>Last Edited by <span id="author">Monty Shokeen</span>
      </p>
      <p id="content">You can change the heading, author name and this content itself. Click on Edit Document to start editing. At this point, you can edit this document and the changes will be saved in localStorage. However, once you reload the page your changes will be gone. To fix it we will have to retrieve the contents from localSotrage when the page reloads.</p>
    </div>
  </section>
</div>

    <script>
    var editBtn = document.getElementById('editBtn');
var editables = document.querySelectorAll('#title, #author, #content');

if (typeof(Storage) !== "undefined") {
  if (localStorage.getItem('title') !== null) {
    editables[0].innerHTML = localStorage.getItem('title');
  }
  if (localStorage.getItem('author') !== null) {
    editables[1].innerHTML = localStorage.getItem('author');
  }
  if (localStorage.getItem('content') !== null) {
    editables[2].innerHTML = localStorage.getItem('content');
  }
}

editBtn.addEventListener('click', function(e) {
  if (!editables[0].isContentEditable) {
    editables[0].contentEditable = 'true';
    editables[1].contentEditable = 'true';
    editables[2].contentEditable = 'true';
    editBtn.innerHTML = 'Save Changes';
    editBtn.style.backgroundColor = '#6F9';
  } else {
    // Disable Editing
    editables[0].contentEditable = 'false';
    editables[1].contentEditable = 'false';
    editables[2].contentEditable = 'false';
    // Change Button Text and Color
    editBtn.innerHTML = 'Enable Editing';
    editBtn.style.backgroundColor = '#F96';
    // Save the data in localStorage 
    for (var i = 0; i < editables.length; i++) {
      localStorage.setItem(editables[i].getAttribute('id'), editables[i].innerHTML);
    }
  }
});
    </script>
</body>

</html>
tanya
  • 21
  • 1
  • 4
  • Would you clarify what you mean by "it does not save into actual HTML code"? Do you mean that your javascript is not modifying the HTML as you expect or that you're wanting this code to be only HTML? – jrader Oct 30 '18 at 14:37
  • Like when u click "save" it exports new file with changes – tanya Oct 30 '18 at 15:08
  • This is not possible using JavaScript in a browser unless you want to use something like NodeJS for your server – Matt Oct 30 '18 at 16:31
  • Is it hard to code with NodeJS then? Can i do it on online hosting? Like Import index.html - / change basic text / save and replace. – tanya Oct 30 '18 at 18:02
  • What are you wanting to export? And in what format? Are you wanting to export the HTML? If so, why? Or are you just wanting to export the content that is being edited? – jrader Oct 30 '18 at 18:54
  • If you could explain the end goal of what you're trying to accomplish that would be very helpful – jrader Oct 30 '18 at 19:01
  • Very simple editor, just to edit text and images, no need for other funkcions. My clients sometimes want to change some text on website and then save it. contentEditable looks perfect for it but problem is that you cant save it. – tanya Oct 30 '18 at 20:00
  • @Tanya The problem then is that you're loading from localStorage. If you want your client's changes to be viewable by others then you need to implement some kind of storage on your server and have your form pull data from that instead. You can still use the local storage for offline mode but you'll have to have a way to sync local storage with your server – jrader Oct 30 '18 at 22:33
  • As example lets take basic editor like Brackets. You can offline preview and edit file. So for my client I dont want them to see all the code and go crazy. So i wanted to make something simple to change text and images. I doenst need to be uploaded somewhere lets say he has index.html on his PC. And after they change everything they need, then they will just click save like in brackets. – tanya Oct 31 '18 at 00:11
  • @tanya I guess I'm confused then because I loaded your example into jsfiddle and it works as you're describing. I was able to edit and save changes just fine – jrader Oct 31 '18 at 15:32
  • @jrader Yes but when you check inside the code there are no changes. – tanya Oct 31 '18 at 16:44
  • @tanya I think maybe I'm starting to understand what you're wanting. You're creating an offline webpage editor that will export the page that they've created as HTML. So that exported page wouldn't necessarily have the HTML you've provided above? Is that correct? If so, I think I know where to point you to next – jrader Oct 31 '18 at 17:01
  • @jrader Yes something like that, i just want them to have option to change text(images if possible) and not worry about code. Then export/save that file. Like very simple offline webpage editor as you said. – tanya Oct 31 '18 at 17:07
  • @jrader Like when i code website for someone and they want to update some text or image they could do it with this "offline webpage editor" then upload it on server back. – tanya Oct 31 '18 at 18:33
  • @tanya I hope my answer posted below helped out. I really love helping other people so if you have more questions we can totally open an email chain or chat or something – jrader Nov 02 '18 at 02:16

1 Answers1

0

You'll want to use something like the downloadInnerHtml function as described here. Ideally you'll probably also want to strip out the script tag and content editable attribute before exporting because you won't want the final html page to be editable

jrader
  • 670
  • 1
  • 5
  • 15