Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to .append text to a div using jQuery?

I'm trying to append a piece of text to a div using jQuery. I try to do this using the following code:

<html><head></head><body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#sendButton").click(function(){
            $("#conversation").append("<P>This is a message");
        });
    });
</script>
<div class="conversation"><p>some message</div>
<form><input type="button" id="sendButton" value="Send Message"></form>
</body></html>

Seeing the multitude of tutorials on the subject it seems to be such a simple thing to do, but I can't seem to figure out what I'm doing wrong here. Any help would be greatly appreciated.

like image 897
kramer65 Avatar asked Feb 05 '14 18:02

kramer65


1 Answers

You need to use class selector, As #conversation referes to element with id conversation

 $(".conversation").append("<P>aergerag");

Fiddle DEMO

EDIT

You should look at this To Close or Not To Close Tags in HTML5 and a good question Closing tags in HTML5

like image 57
Satpal Avatar answered Sep 19 '22 19:09

Satpal