-1

So I want to build chat message inside list. I want to add new list after user input the message in input field and click submit button.

Here is my HTML:

<div class="panel-collapse">
    <div class="panel-body" style="overflow-y: scroll; height: 250px;">
        <ul class="chat" id="list"> 
           <li class="left clearfix">
                <span class="chat-img pull-left">
                    <img src="http://placehold.it/50/55C1E7/fff&text=U" alt="User Avatar" class="img-circle" />
                </span>
                <div class="chat-body clearfix">
                     <div class="header">
                         <strong class="primary-font">USER NAME</strong> <small class="pull-right text-muted">
                         <span class="glyphicon glyphicon-time"></span>12 mins ago</small>
                      </div>
                      <p> PUT HERE SOME OF TEXT OF CHAT </p>
                </div>
           </li>
           <li> ALL ATTRIBUTES IN PREVIOUS LI AND NEW MESSAGE WILL BE HERE </li>
        </ul>
    </div>

    <div class="panel-footer">
        <div class="input-group">
            <input id="chat-input" type="text" class="form-control input-sm" placeholder="Type your message here..." />
            <span class="input-group-btn">
                <button class="btn btn-warning btn-sm" id="btn-chat" type="submit"> Send</button>
            </span>
         </div>
    </div>

</div>

So everytime user text some message inside <input> field, and click button submit, jquery will add new <Li> and the message will put in <p> </p>.

Here is my JQuery:

$(document).ready(function() {
    $('#btn-chat').click(function(e) {
        e.preventDefault();
         var new_chat = $('#chat-input').val();
        $('#list').append('<p>'+new_chat+'</p>');
    });
});

Please correct my code if I'm do something wrong. I really appreciate it. Thank you.

Lazy Guy
  • 101
  • 2
  • 14
  • Have you simply tried changing this `$('#list').append('

    '+new_chat+'

    ');` to this: `$('#list').append('
  • '+new_chat+'
  • ');`? – Scott Marcus Mar 17 '16 at 15:55
  • solved, thanks everyone :) thanks @CMedina – Lazy Guy Mar 17 '16 at 16:00
  • @LazyGuy ok!. Accept the Answer and close Question! – CMedina Mar 17 '16 at 16:03