Search code examples
htmlcontenteditable

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?


Solution

  • 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()'>...