Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    document.createHTMLElement()

    Well, while working on a recent project, I created a method for making DOM-approved™ HTML elements fairly easily. Here's the method
    Code:
    document.createHTMLElement = function( elemName, attribs )
    {
        var elem = document.createElement( elemName );
        if ( typeof attribs != 'undefined' )
        {
            for ( var i in attribs )
            {
                switch ( true )
                {
                    case ( i == 'text' )  : elem.appendChild( document.createTextNode( attribs[i] ) ); break;
                    case ( i == 'class' ) : elem.className = attribs[i]; break;
                    default : elem.setAttribute( i, '' ); elem[i] = attribs[i];
                }
            }
        }
        return elem;    
    }
    I also cooked up a little demo or two

    To use this, you will need to be familiar with object literals
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    If the document is XHTML, you'll need to use createElementNS("http://www.w3.org/1999/xhtml", elemName) for the element. Fortunately, you shouldn't use the namespaced-attribute methods as well.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Which, with all due respect, I think should be split so that comments regarding my post are easier to find, and so that all the other good information here isn't buried osbcurely in the "Post a Javascript" forum.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    *performs the first-ever thread split of CodingForums*

    good stuff on namespaces and the DOM

    To quote Invader Zim, "Feel honored!"


  •  

    Posting Permissions

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