JavaScript Unable to append jquery

I am unable to append the page just seems to refresh where as i want to view it without refreshing the page

index.php

<div class="view_comment">
        <b>username :</b> <?php echo  $comment_comment;?>
</div> 
<div id="comment_type_area" class="comment_type_area">
        <form method="POST">
            <input type="text" class="comment" post_id="<?php echo $shared_id2; ?>" id="text_comment" placeholder="Write a Comment"></input>
            <input type="submit" id="post_button" ></input> 
        </form>
        </div>

Jquery.js

  $(document).ready(function(){

        $('.comment').keydown(function (e){
            if(e.keyCode == 13){
                var post_id = $(this).attr('post_id');
                var comment = $(this).val();
                $.post('/comment.php',{ post_id: post_id, comment:comment});
                $('.comment').val('');
/*i am guessing the problem starts from here and onwards*/
                $(this).parent().children('.comments').append("<div class='view_comment'><b>Username :</b>" + comment +"</div>");
            }
        });

    });
Answer:1

You should add e.preventDefault() to prevent the submit since when you click enter button in fields of form that will submit automatically the form :

if(e.keyCode == 13){
     e.preventDefault();

     var post_id = $(this).attr('post_id');
     var comment = $(this).val();
     ...
}

Note : input is one of self-closed tags so it should be <input type="submit" id="post_button" />.

Hope this helps.

Answer:2

So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function ...

So I'm working on a WPF app that has a web page self hosting, and in my Javascript I need to use some data from my C#. I'm wondering if there's any way (in WPF/C#) to invoke a Javascript function ...

JSFIDDLE: https://jsfiddle.net/uj3uw9cp/1/ body { color: white; } a { color: white; text-decoration: none; } #text-on-image { overflow: ...

JSFIDDLE: https://jsfiddle.net/uj3uw9cp/1/ body { color: white; } a { color: white; text-decoration: none; } #text-on-image { overflow: ...

I work on an Ember CLI project that has Ember Data declared as a dependency in the the project's package.json and bower.json files respectively. Two different Ember Data versions are declared. Which ...

I work on an Ember CLI project that has Ember Data declared as a dependency in the the project's package.json and bower.json files respectively. Two different Ember Data versions are declared. Which ...

  1. ember package.json
  2. ember-cli package.json
  3. ember init package.json
  4. ember require package.json
  5. package.json ember-addon

I have added some input fields in a form using counter method, and I saved that form. When I am trying to add fields again in to the form, the counter is starting from 0. For example, I added 5 input ...

I have added some input fields in a form using counter method, and I saved that form. When I am trying to add fields again in to the form, the counter is starting from 0. For example, I added 5 input ...

  1. resume for counter sales
  2. resume for counter person
  3. resume of counter staff
  4. resume for counter checker
  5. resume for counterintelligence