SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting an element's HTML

    hi.

    is there a way to get the HTML representation of a newly created object?

    like, if I do

    HTML Code:
    var h1 = document.createElement('h1');
    var txt = document.createTextNode('hello!');
    h1.appendChild(txt);
    how do I get
    Code:
    <h1>hello!</h1>
    thanks, any help appreciated!

  2. #2
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want <h1>hello!</h1> to display on screen?

    I added these lines of js to make the text "hello!" append to an element with an id

    var tag = document.getElementById("tag");
    tag.appendChild(h1);

    with this corresponding html and css:

    <style>
    h1{
    display: inline;
    }
    </style>
    </head>
    <body>
    <code>&lt;h1&gt;<span id="tag"></span>&lt;/h1&gt;</code>
    </body>
    </html>

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In internet explorer:
    Code:
    alert(h1.outerHTML);
    Other browsers don't implement this. You'd need to write some kind of custom function to create the tag with all its attributes. Or if you're using jQuery check out
    http://blog.brandonaaron.net/2007/06...ets-outerhtml/

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2008
    Location
    Asheville, NC
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    thanks!

    outerHTML is what I was looking for;

    it's ok if it's supported by IE only, it's a piece of code just for this browser.

    thanks!


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
  •