0

I have separate reply form with each post with it's own php $id in same page. So I want to submit each reply with keyboard Enter key. I collect this javascript code below from here, but I cannot understand how apply it for each reply.

B.N: Collected code not working also.

my form:

<form action="" method="post" class="repfrm'.$id.'" id="prepfrm">
<textarea name="replycom" id="replycom'.$id.'" class="replycom"></textarea>
<button type="submit" name="submit" id="'.$id.'" class="replyfrm">Post Reply</button>
</form>

Collected Script From stockoverflow:

$(".replycom").keyup(function(event){
   if(event.keyCode == 13){
       $(".replyfrm").click();
   }
});
koc
  • 845
  • 8
  • 22

3 Answers3

0

Something like that:

Bind keypress to textarea and submit the closest form when press the key

$("textarea").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $(this).closest("form").submit();
    }
});
nada
  • 962
  • 5
  • 22
0

Try this script

 $(".replycom").keypress(function (e) {
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
        }

    });
Anirudha Gupta
  • 8,248
  • 8
  • 49
  • 71
0

Change textarea to textbox and you press enter in textBox then it works which you want. Do not required jQuery code.

<form action="" method="post" class="repfrm'.$id.'" id="prepfrm">
   <input type="text" name="replycom" id="replycom'.$id.'" class="replycom">
   <button type="submit" name="submit" id="'.$id.'" class="replyfrm">Post Reply</button>
</form>
Sadikhasan
  • 17,212
  • 19
  • 72
  • 111
  • I have some css code with textarea, also I used $('textarea').elastic(); for auto height. so maybe I cannot change textarea to textbox. But I will try it also. Thank U. – koc Feb 03 '15 at 14:42