DIV parameter value with jQuery

I am just trying to create a chat app with jQuery and HTML, so I am using two HTML elements:

<div id="chat" style="height: 359px; width: 146px" > 
<p id="answer">
</p>
</div>
<input id="send" type="text" value="ارسال" style="width: 120px"/>

      

and this jQuery function:

$(document).keypress(function(e) {
    if(e.which == 13) {
       //Get

    var bla = $('#send').val();
    bla = bla +  $('#answer').val();


    //Set
    $('#answer').val(bla);

    }
});

      

I tried this code with text instead of a div element and it kind of worked, but when I add a div element it doesn't work anymore. Now I want to do two things: 1. Adding #send text to the #answer element - not replacing it. 2.Create a blank line after each input.

+3


source to share


1 answer


Take a look at the fiddle .

Change .val()

on #answer

to .html()

.

To get data inside div

or p

, is used .html()

.



Here is a snippet.

$(document).keypress(function(e) {
  if (e.which == 13) {
    //Get

    var bla = $('#send').val();
    bla = bla + $('#answer').html();


    //Set
    $('#answer').html(bla);

  }
});
      

<div id="chat" style="height: 359px; width: 146px">
  <p id="answer">
  </p>
</div>
<input id="send" type="text" value="ارسال" style="width: 120px" />
      

Run codeHide result


+1


source







All Articles