How to .append text to a div using jQuery?

15,949

Solution 1

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

Solution 2

replace # with . in your selector (conversation is a CLASS)

$(".conversation").append("<P>aergerag");
Share:
15,949
kramer65
Author by

kramer65

Updated on June 25, 2022

Comments

  • kramer65
    kramer65 almost 2 years

    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.