<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dom code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 30 Aug 2008 09:11:20 GMT</pubDate>
    <description>DZone Snippets: dom code</description>
    <item>
      <title>create pre-styled/populated DOM elements</title>
      <link>http://snippets.dzone.com/posts/show/61</link>
      <description>&lt;code&gt;&lt;br /&gt;/*&lt;br /&gt; * create a new DOM element with (optionally) specified className, CSS and text/html content&lt;br /&gt; * if text contains any markup, it's used as innerHTML value - else a child text node is attached&lt;br /&gt; * if there's also a parent node given, the new element will be appended as child node&lt;br /&gt; */&lt;br /&gt;function createStyledElement(tag, parent, cls, css, txt) {&lt;br /&gt;    var el = document.createElement(tag);&lt;br /&gt;    if(cls) el.className = cls;&lt;br /&gt;    if(css) for(var s in css) el.style[s]=css[s];&lt;br /&gt;    if(txt) {&lt;br /&gt;        if (txt.indexOf('&lt;')!=-1) el.innerHTML=txt;&lt;br /&gt;        else el.appendChild(document.createTextNode(txt));&lt;br /&gt;    }&lt;br /&gt;    if(parent) parent.appendChild(el);&lt;br /&gt;    return el;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// usage example...&lt;br /&gt;// 1st define some style params&lt;br /&gt;var style1={&lt;br /&gt;    background: '#def',&lt;br /&gt;    color: '#f00',&lt;br /&gt;    padding: '0.5em',&lt;br /&gt;    border: '1px black dotted'&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;createStyledElement(&lt;br /&gt;  'div',&lt;br /&gt;  document.getElementsByTagName('body').item(0),&lt;br /&gt;  null,&lt;br /&gt;  style1,&lt;br /&gt;  'hello &lt;em&gt;world&lt;/em&gt;!'&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Apr 2005 23:18:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/61</guid>
      <author>toxi (karsten schmidt)</author>
    </item>
  </channel>
</rss>
