title: jQuery Ajax two identical functions working for one and not the other
body: <p>I am making a comment system where a user can comment something to a post and then other users can reply to that comment. I have made a function to comment which works perfectly. I also wanted a similar function to work for that reply to comment part so I copied the same function and made some changes to the code like variable names etc. But its not working. When I write a reply and press enter it takes me to a new line rather than submitting. Please help me.</p>
  
  <pre><code>// COMMENT UPDATE (WORKING)
  
  $(document).on('keydown', '.commentarea', function(event) {
      var comtt = $(".commentarea").val();
      if(comtt!=''){
          if(event.keyCode == 13 &amp;&amp; !event.shiftKey) {
              var id = $(this).attr('id');
              var status_id = id.replace("postcomment_", "");
              createComment(status_id);
              event.preventDefault();
          }
      }
  });
  
  function createComment(status_id){
    var comment = $('#postcomment_'+status_id).val();
    $.ajax({
        url: "post-comment.php",type: "POST",
        data: {comment : comment, statsid : status_id},
        success: function (data) {
          $("#showbox_"+status_id).append(data);
        },
        error: function () {
          alert("Ooops!! Problem Ocurred. Please try again later. If problem persists, please contact support!");
        },
        complete: function(xhr) {
          $('#postcomment_'+status_id).val('');
        }
    })
  }
  
  // REPLY TO COMMENT (SAME FUNCTION BUT NOT WORKING)
  
  $(document).on('keydown', '.replyarea', function(event) {
      var comtr = $(".replyarea").val();
      if(comtr!=''){
          if(event.keyCode == 13 &amp;&amp; !event.shiftKey) {
              var rid = $(this).attr('id');
              var commentId = rid.replace("replycomment_", "");
              createReply(commentId);
              event.preventDefault();
          }
      }
  });
  
  function createReply(commentId){
    var creply = $('#replycomment_'+status_id).val();
    $.ajax({
        url: "post-replies.php",type: "POST",
        data: {creply : creply, cmtid : commentId},
        success: function (data) {
          $("#showreply_"+commentId).append(data);
        },
        error: function () {
          alert("Ooops!! Problem Ocurred. Please try again later. If problem persists, please contact support!");
        },
        complete: function(xhr) {
          $('#replycomment_'+commentId).val('');
        }
    })
  }
  </code></pre>
  
  <p>PHP Part:</p>
  
  <pre><code>&lt;?php while($get_stf = $get_stq-&gt;fetch()){ extract($get_stf); // fetch posts ?&gt;
  
  &lt;?php while($fetch_cmts = $get_cmtq-&gt;fetch()){ extract($fetch_cmts); // fetch comments?&gt;
  
   &lt;!-- Reply to comments form --&gt;
  
  &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" class="full-width" id="cmt_form_id_&lt;?php echo $cmt_id; ?&gt;"&gt;
  
  &lt;input type="hidden" name="comment_id" value="&lt;?php echo $cmt_id; ?&gt;" id="cmtsid_&lt;?php echo $cmt_id; ?&gt;" /&gt;
  
  &lt;textarea name="reply" placeholder="Give a reply..." class="reply-comment-field replyarea" id="replycomment_&lt;?php echo $cmt_id; ?&gt;"&gt;&lt;/textarea&gt;
  
  &lt;/form&gt;
  &lt;!-- End Reply to comments form --&gt;
  
  &lt;?php } ?&gt;
  
  &lt;!-- Comment form --&gt;
  &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" class="full-width" id="cmt_form_id_&lt;?php echo $cmt_id; ?&gt;"&gt;
  
  &lt;input type="hidden" name="comment_id" value="&lt;?php echo $cmt_id; ?&gt;" id="cmtsid_&lt;?php echo $cmt_id; ?&gt;" /&gt;
  
  &lt;textarea name="reply" placeholder="Give a reply..." class="reply-comment-field replyarea" id="replycomment_&lt;?php echo $cmt_id; ?&gt;"&gt;&lt;/textarea&gt;
  
  &lt;/form&gt;
  &lt;!-- End Comment form --&gt;
  
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>Both functions are same. But one being used for COMMENTS is working perfectly whereas the one used for REPLY TO COMMENTS is not working. It should submit the form and insert the data when I press enter in REPLY TO COMMENTS form after writing some text just like FACEBOOK. Please help.</p>