SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add dom element in html using jquery

    Hello
    Please can someone tell me why this does not work

    $('<div>Hello where is my soy latte?</div>'); inorder to add a Dom element to the page

    this is an eg from the book Jquery Novice to ninja
    Thanks
    Meera

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    $('<div>Hello where is my soy latte?</div>').appendTo('body');

    You need to append it or prepend it to an element. Right now you are creating it, but not actually telling jQuery where to put it

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or simply
    $('body').html('<div>Hello where is my soy latte?</div>')

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many Thanks for your prompt answer
    Meera

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,217
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    or simply
    $('body').html('<div>Hello where is my soy latte?</div>')
    If you use this approach you will overwrite your existing HTML, since you specifically wanted to add to the dom, I suggest using appendTo or prependTo

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    If you use this approach you will overwrite your existing HTML, since you specifically wanted to add to the dom, I suggest using appendTo or prependTo
    just for learning purpose
    and
    $(sel).html(html)
    is the simplest way to add html to the dom


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •