Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML5, how do I use contenteditable fields in a form submission?

So I have a bunch of paragraph elements which are dynamically populated from a db. I have made the elements contenteditable. I now want to submit edits back the the db via a standard form submission. Is there a way to post the contenteditable elements back?

like image 772
MFB Avatar asked Jun 06 '11 04:06

MFB


People also ask

How do you use Contenteditable in HTML?

Definition and UsageThe contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.

How do you use Contenteditable?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

What is the HTML attribute Contenteditable used for?

The contenteditable global attribute is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing.

How do you use Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.


2 Answers

You have to use javascript one way or the other, it won't work as a "standard" form element as it would with a textarea or the like. If you like, you could make a hidden textarea within your form, and in the form's onsubmit function copy the innerHTML of the contenteditable to the textarea's value. Alternatively you could use ajax/xmlHttpRqeuest to submit the stuff a bit more manually.

function copyContent () {     document.getElementById("hiddenTextarea").value =           document.getElementById("myContentEditable").innerHTML;     return true; }   <form action='whatever' onsubmit='return copyContent()'>... 
like image 74
rob Avatar answered Sep 16 '22 17:09

rob


If anyone is interested I patched up a solution with VueJS for a similar problem. In my case I have:

<h2 @focusout="updateMainMessage" v-html="mainMessage" contenteditable="true"></h2> <textarea class="d-none" name="gift[main_message]" :value="mainMessage"></textarea> 

In "data" you can set a default value for mainMessage, and in methods I have:

methods: {   updateMainMessage: function(e) {     this.mainMessage = e.target.innerText;   } } 

"d-none" is a Boostrap 4 class for display none. Simple as that, and then you can get the value of the contenteditable field inside "gift[main_message]" during a normal form submit for example, no AJAX required. I'm not interested in formatting, therefore "innerText" works better than "innerHTML" for me.

like image 44
Alberto T. Avatar answered Sep 20 '22 17:09

Alberto T.