user3886042 user3886042 - 7 months ago 39
Javascript Question

submit appended form containing a contenteditable div on press of enter key

i want to submit a form which contains a contenteditable div on press of the enter key. the form is appended into a div using jquery .before().

<form class="send" id="send_it<?php echo $seed['idd']; ?>" method="post" action="#">
<div id="input_msg" contenteditable="true"
class="input<?php echo $seed['idd']; ?>"
style="width:210px; position:fixed; bottom:0;">
</div>
<input type="submit" value="send" style="display:none;" />
</form>

Answer

this will do what you want:

$(document).keyup(function(e){
    if(e.keyCode== 13 || e.which== 13) { //if enter key is pressed
        var contentEditableValue=$('#input_msg').text(); //get the div value
        $('.send').append('<input type="hidden" value="'+contentEditableValue+'" id="valueToSend">'); //add a dummy input to the form to send the value
        $('.send').submit(); //submit the form
    }
});