30

Can <div contenteditable="true"><a href="page.html">Some</a> Text</div> be used instead of texarea and then passed trough form somehow?

Ideally without JS

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
JDDll
  • 301
  • 1
  • 3
  • 3
  • 1
    possible duplicate of [Using HTML5, how do I use contenteditable fields in a form submission?](http://stackoverflow.com/questions/6247702/using-html5-how-do-i-use-contenteditable-fields-in-a-form-submission) – dbn Apr 11 '14 at 07:04
  • imho, i reckon `contenteditable` almost useless, as it cant be included into forms like other inputs. you will need bunch of JS to make it happen. – T.Todua Jan 21 '21 at 18:25

4 Answers4

26

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

Content Editable does not work as a form element. Only javascript can allow it to work.

EDIT: In response to your comment... This should work.

<script>
    function getContent(){
        document.getElementById("my-textarea").value = document.getElementById("my-content").innerHTML;
    }
</script>


<div id="my-content" contenteditable="true"><a href="page.html">Some</a> Text</div>

<form action="some-page.php" onsubmit="return getContent()">
    <textarea id="my-textarea" style="display:none"></textarea>
    <input type="submit" />
</form>

I have tested and verified that this does work in FF and IE9.

smdrager
  • 7,327
  • 6
  • 39
  • 49
  • what should i do with JS (not jquerry) to make it work? document.getElementById('contentTextArea').value = document.getElementById('contentDiv').value; this dont work – JDDll Sep 08 '11 at 23:37
  • Hmm that's true, div's don't have a "value" attribute. I'll update my answer in a moment. – smdrager Sep 09 '11 at 00:21
  • so security-wise, `contentedtiable` is safe? no need to ignore it because its already ignored? – oldboy Jul 20 '19 at 23:27
  • what alternates are there if the text is large and it freezes the text area due to memory or character limit, like importing a 1MB csv with 2 columns – Muhammad Omer Aslam Sep 04 '19 at 05:58
3

You could better use:

<script>
    function getContent(){
        document.getElementById("my-textarea").value = document.getElementById("my-content").innerText;
    }
</script>

NOTE: I changed innerHTML to innerText. This way you don't get HTML elements and text but only text.

Example: I submited "text", innerHTML gives the value: "\r\n text". It filters out "text" but it's longer then 4 characters. innerText gives the value "text".

This is useful if you want to count the characters.

Nash Carp
  • 169
  • 12
2

Try out this

document.getElementById('formtextarea').value=document.getElementById('editable_div').innerHTML;

a full example:-

<script>
    function getContent() {
        var div_val = document.getElementById("editablediv").innerHTML;
        document.getElementById("formtextarea").value = div_val;
        if (div_val == '') {
            //alert("option alert or show error message")
            return false;
            //empty form will not be submitted. You can also alert this message like this.
        }
    }
</script>

`

<div id="editablediv" contenteditable="true">
<a href="page.html">Some</a> Text</div>
<form id="form" action="action.php" onsubmit="return getContent()">
    <textarea id="formtextarea" style="display:none"></textarea>
    <input type="submit" />
</form>

`

Instead of this, you can use JQuery (if there is boundation to use JQuery for auto-resizing textarea or any WYSIWYG text editor)

Ravinder Payal
  • 2,884
  • 31
  • 40
0

Without JS it doesn't seem possible unfortunately. 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. I'm not interested in formatting, therefore "innerText" works better than "innerHTML" for me.

Alberto T.
  • 487
  • 3
  • 8