Anon
Anon

Reputation: 865

innerHTML value of contenteditable div on modification

I have a content editable div:

<div contenteditable="true" id="my-div">
<? if(isset($data["description"])) {
       print_r($data["description"]);
} ?>
</div>

Once the user edits the div I am trying to pass the modified value of the div to a hidden form element.

document.getElementById("desc").value =document.getElementById("my-div").innerHTML();

I am getting the original value on page load, not the modified value as innerHTML.

How do I get the updated value?

Upvotes: 2

Views: 6258

Answers (1)

thecotne
thecotne

Reputation: 478

$('#my-div').on('keypress', function() {
    $('#desc').val($(this).html());
});

you need to listen to changes to the editable div and update input value

i assume you use jquery if not

document.getElementById('my-div').addEventListener('keypress', function(e) {
    document.getElementById('desc').value = this.innerHTML;
});

Upvotes: 4

Related Questions