Paul Hopkinson
Paul Hopkinson

Reputation: 441

getting the value from textarea into a div

I have a div -

<div id="chatText" class="chatText">Welcome</div>

and a textarea and button

New Message<br />
<textarea id="newChatText1" rows="3" cols="50" class="chatinput"></textarea>
    <br />
    <input type="button" name="submit" value="submit" onclick="AddContent()">

I want the user to be able to put content into the textarea and it get displayed and ADDED to the existing div content

So if the user entered 'Hello' into the textarea and pressed submit the div would display

'Welcome Hello'

Current js is

function AddContent(){
    var text1 = $("#chatText").val();           
    var text2 = $("#newChatText1").val();   
    $("#chatText").text(text1+text2);   
}

Any ideas?

Thanks

Paul

Upvotes: 0

Views: 54

Answers (2)

epascarello
epascarello

Reputation: 207501

A div does not have a value

var text1 = $("#chatText").val(); 

needs to be text()

var text1 = $("#chatText").text(); 

For the new line, I would do this instead.

function AddContent(){
    var text2 = $("#newChatText1").val();   
    var p = $("<p></p>").text(text2);
    $("#chatText").append(p);   
}

Upvotes: 3

Stefano P.
Stefano P.

Reputation: 179

You can use .append() method to do that.

function addContent(){
    $("#chatText").append( $("#newChatText1").val() );
}

Upvotes: 0

Related Questions