2

I am doing a chat project in java.

Using my Server response through jquery-ajax , I am creating an html and append the html to div.

My jquery - html alert without empty string-name(note the id='user' in the alert) will be ,

enter image description here

after appending this in the chat area ,

enter image description here

After jquery-html alert with empty string-name(note the id='user' in the alert) will be ,

enter image description here

after appending this in the chat area ,

enter image description here

My jquery for creating the HTML will be ,

function send(name , message , time){
    //name = $("#name").val();
    //message = $("#message").val();

    var user = "<div id='user' class='fontStyle'>"+name+"</div>";
    var msg = "<div id='msg'  class='fontStyle'>"+message+"</div>";
    var timeStamp = "<div id='time'  class='fontStyle'>"+time+"</div>";

    var row = "<div id='msgSet' >"+ user + msg + timeStamp +"</div>";

    alert(row);
    return row;

}

There is no empty string will be present in chat area.

Hope our stack members will help me.

Human Being
  • 8,269
  • 28
  • 93
  • 136

1 Answers1

0

You can escape html your string so it keeps the white spaces. See details here.

Or you can trim your message string (=remove spaces at the beginning and end of it) with such an instruction, and then manualy add the spaces (with the &nbsp; code inside a div, span, ...):

s.replace(/\s+/g, ' ');
Community
  • 1
  • 1
Ricola3D
  • 2,402
  • 17
  • 16