<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:series="http://unfoldingneurons.com/"
> <channel><title>Comments on: MooTools-Like Element Creation in&#160;jQuery</title> <atom:link href="http://davidwalsh.name/jquery-create-element/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name/jquery-create-element</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Wed, 23 May 2012 04:02:29 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: David Walsh</title><link>http://davidwalsh.name/jquery-create-element#comment-14628</link> <dc:creator>David Walsh</dc:creator> <pubDate>Fri, 18 Dec 2009 14:11:41 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-14628</guid> <description>@mits:  AWESOME!  I love it!</description> <content:encoded><![CDATA[<p>@mits:  AWESOME!  I love it!</p> ]]></content:encoded> </item> <item><title>By: mits</title><link>http://davidwalsh.name/jquery-create-element#comment-14623</link> <dc:creator>mits</dc:creator> <pubDate>Fri, 18 Dec 2009 10:29:54 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-14623</guid> <description>hello,this is a good idea, but my function is easier than your:
(function($){
$.fn.create = function(element, attributes){
return $(document.createElement(element)).attr(attributes).prependTo(this);
};
})(jQuery);usage:
$(&#039;body&#039;).create(&#039;div&#039;, {&#039;id&#039;:&#039;sampleId&#039;, &#039;class&#039;:&#039;normal&#039;}).text(&#039;sample&#039;);</description> <content:encoded><![CDATA[<p>hello,</p><p>this is a good idea, but my function is easier than your:<br
/> (function($){<br
/> $.fn.create = function(element, attributes){<br
/> return $(document.createElement(element)).attr(attributes).prependTo(this);<br
/> };<br
/> })(jQuery);</p><p>usage:<br
/> $(&#8216;body&#8217;).create(&#8216;div&#8217;, {&#8216;id&#8217;:'sampleId&#8217;, &#8216;class&#8217;:'normal&#8217;}).text(&#8216;sample&#8217;);</p> ]]></content:encoded> </item> <item><title>By: Ryan Rampersad</title><link>http://davidwalsh.name/jquery-create-element#comment-12487</link> <dc:creator>Ryan Rampersad</dc:creator> <pubDate>Tue, 06 Oct 2009 12:55:41 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12487</guid> <description>I find the mootools element creation clunky so I use the string substitution that was mentioned in the comments too, it&#039;s quick and easy, plus I get a bunch of elements quick.That said, I can see how it would be nice to map a comfortable syntax to an uncomfortable one in situations where there is no other remedy.</description> <content:encoded><![CDATA[<p>I find the mootools element creation clunky so I use the string substitution that was mentioned in the comments too, it&#8217;s quick and easy, plus I get a bunch of elements quick.</p><p>That said, I can see how it would be nice to map a comfortable syntax to an uncomfortable one in situations where there is no other remedy.</p> ]]></content:encoded> </item> <item><title>By: Jedidiah</title><link>http://davidwalsh.name/jquery-create-element#comment-12486</link> <dc:creator>Jedidiah</dc:creator> <pubDate>Tue, 06 Oct 2009 11:21:54 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12486</guid> <description>Would be nice if you could stick to the css syntax instead.create(&#039;li#item.custom-class&#039;, &#039;content of the element here&#039;);</description> <content:encoded><![CDATA[<p>Would be nice if you could stick to the css syntax instead</p><p>.create(&#8216;li#item.custom-class&#8217;, &#8216;content of the element here&#8217;);</p> ]]></content:encoded> </item> <item><title>By: Steve Calvert</title><link>http://davidwalsh.name/jquery-create-element#comment-12472</link> <dc:creator>Steve Calvert</dc:creator> <pubDate>Mon, 05 Oct 2009 20:33:13 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12472</guid> <description>@Aeron - well said.</description> <content:encoded><![CDATA[<p>@Aeron &#8211; well said.</p> ]]></content:encoded> </item> <item><title>By: Aeron</title><link>http://davidwalsh.name/jquery-create-element#comment-12470</link> <dc:creator>Aeron</dc:creator> <pubDate>Mon, 05 Oct 2009 19:51:38 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12470</guid> <description>Mootools doesn&#039;t do overloading: getStyle(rule), setStyle(rule, val), setStyles(obj), etc.. With jQuery everything is overloaded - a single css() does all of the above depending on whether 1 or 2 params are passed and whether they are objects or strings.So it totally makes sense to do $(&#039;li&#039;) to find all the LI&#039;s in a page, and $(&#039;&#039;) to create a new LI.I came from a Mootools background to a huge jQuery project - first thing I did was port over a bunch of Mootools goodies: bind and pass (which look to be coming in the next version of jQuery) and Array, String and Number helpers... but the goal is understanding the tools you are given. It is the difference between someone who is a year in another country and everytime they speak think &quot;how would I say this in English&quot; and then translate - vs - someone who is 10 years in another country and just speaks the language fluently. $.create() is not increasing your toolbox, it&#039;s rewriting a piece of a foreign language in English.</description> <content:encoded><![CDATA[<p>Mootools doesn&#8217;t do overloading: getStyle(rule), setStyle(rule, val), setStyles(obj), etc.. With jQuery everything is overloaded &#8211; a single css() does all of the above depending on whether 1 or 2 params are passed and whether they are objects or strings.</p><p>So it totally makes sense to do $(&#8216;li&#8217;) to find all the LI&#8217;s in a page, and $(&#8221;) to create a new LI.</p><p>I came from a Mootools background to a huge jQuery project &#8211; first thing I did was port over a bunch of Mootools goodies: bind and pass (which look to be coming in the next version of jQuery) and Array, String and Number helpers&#8230; but the goal is understanding the tools you are given. It is the difference between someone who is a year in another country and everytime they speak think &#8220;how would I say this in English&#8221; and then translate &#8211; vs &#8211; someone who is 10 years in another country and just speaks the language fluently. $.create() is not increasing your toolbox, it&#8217;s rewriting a piece of a foreign language in English.</p> ]]></content:encoded> </item> <item><title>By: thomasd</title><link>http://davidwalsh.name/jquery-create-element#comment-12469</link> <dc:creator>thomasd</dc:creator> <pubDate>Mon, 05 Oct 2009 18:46:36 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12469</guid> <description>@james: you&#039;re totally right, nothing is as fast as innerHTML, that&#039;s what browsers are best in, parsing HTML-text to HTML in the DOM. What I tried to say is, that there isn&#039;t a jQuery or a mootools way of creating elements. It&#039;s parsing (innerHTML) on the one side and object-oriented creating of elements (document.createElement) on the other side and you can&#039;t compare them directly. Two different ways of doing the same.I didn&#039;t say innerHTML adds another JS-layer. What I said was mootools doesn&#039;t add another JS layer it uses the object-oriented way. Not better or bader just different.For me using CSS selectors to create elements is something between this two methods. Expressive strings to make it a little easier to read and write, but still be objected-oriented. Still not better or bader but definitly slower than innerHTML, I agree with you. But JS-engines become faster and faster and speed isn&#039;t always the most importing thing.Still...mootools rocks :)
Oh and sorry for my bad English, it isn&#039;t my native language.</description> <content:encoded><![CDATA[<p>@james: you&#8217;re totally right, nothing is as fast as innerHTML, that&#8217;s what browsers are best in, parsing HTML-text to HTML in the DOM. What I tried to say is, that there isn&#8217;t a jQuery or a mootools way of creating elements. It&#8217;s parsing (innerHTML) on the one side and object-oriented creating of elements (document.createElement) on the other side and you can&#8217;t compare them directly. Two different ways of doing the same.</p><p>I didn&#8217;t say innerHTML adds another JS-layer. What I said was mootools doesn&#8217;t add another JS layer it uses the object-oriented way. Not better or bader just different.</p><p>For me using CSS selectors to create elements is something between this two methods. Expressive strings to make it a little easier to read and write, but still be objected-oriented. Still not better or bader but definitly slower than innerHTML, I agree with you. But JS-engines become faster and faster and speed isn&#8217;t always the most importing thing.</p><p>Still&#8230;mootools rocks :)<br
/> Oh and sorry for my bad English, it isn&#8217;t my native language.</p> ]]></content:encoded> </item> <item><title>By: Paul Irish</title><link>http://davidwalsh.name/jquery-create-element#comment-12467</link> <dc:creator>Paul Irish</dc:creator> <pubDate>Mon, 05 Oct 2009 16:53:27 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12467</guid> <description>I, too, prefer writing HTML as a string instead of this object representation, but I can see the allure in it (despite its performance overhead).Here&#039;s a little snippet I&#039;ve used a lot when desiring this object-y style:
&lt;code&gt;
/* Return a DOM element with tag name `elem` and attributes {attrs}. */
function DOM(elem, attrs) {
var elem = document.createElement(elem);
for (var attr in attrs) {
elem[attr] = attrs[attr];
}
return elem;
}
&lt;/code&gt;It provides nearly exactly the same API as the above code (though probably not as full-featured)...  As an example:&lt;code&gt;
var element = DOM(&#039;li&#039;,{
id: &#039;item&#039;,
&#039;className&#039;: &#039;custom-class&#039;,
&#039;innerHTML&#039;: &#039;This is a new LI element&#039;
});
&lt;/code&gt;Also, (moreso from the comments on Basil&#039;s post..) I wanted to point out that throwing jQuery a big string of HTML is faster than using all the methods...So:
&lt;code&gt;
$(&#039;#wrapper&quot;).append(&#039;hello world&#039;);
&lt;/code&gt;
is faster than
&lt;code&gt;
jQuery(&quot; &quot; ).attr( &quot;id&quot;, &quot;wrapper-header&quot;).addClass( &quot;header&quot; ).html( &quot;hello world&quot;).appendTo( jQuery( &quot;#wrapper&quot; ) );
&lt;/code&gt;Cheers</description> <content:encoded><![CDATA[<p>I, too, prefer writing HTML as a string instead of this object representation, but I can see the allure in it (despite its performance overhead).</p><p>Here&#8217;s a little snippet I&#8217;ve used a lot when desiring this object-y style:<br
/> <code><br
/> /* Return a DOM element with tag name `elem` and attributes {attrs}. */<br
/> function DOM(elem, attrs) {<br
/> var elem = document.createElement(elem);<br
/> for (var attr in attrs) {<br
/> elem[attr] = attrs[attr];<br
/> }<br
/> return elem;<br
/> }<br
/> </code></p><p>It provides nearly exactly the same API as the above code (though probably not as full-featured)&#8230;  As an example:</p><p><code><br
/> var element = DOM('li',{<br
/> id: 'item',<br
/> 'className': 'custom-class',<br
/> 'innerHTML': 'This is a new LI element'<br
/> });<br
/> </code></p><p>Also, (moreso from the comments on Basil&#8217;s post..) I wanted to point out that throwing jQuery a big string of HTML is faster than using all the methods&#8230;</p><p>So:<br
/> <code><br
/> $('#wrapper").append('hello world');<br
/> </code><br
/> is faster than<br
/> <code><br
/> jQuery(" " ).attr( "id", "wrapper-header").addClass( "header" ).html( "hello world").appendTo( jQuery( "#wrapper" ) );<br
/> </code></p><p>Cheers</p> ]]></content:encoded> </item> <item><title>By: jQueryGlobe</title><link>http://davidwalsh.name/jquery-create-element#comment-12464</link> <dc:creator>jQueryGlobe</dc:creator> <pubDate>Mon, 05 Oct 2009 15:38:53 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12464</guid> <description>seems like some tags are stripped ...</description> <content:encoded><![CDATA[<p>seems like some tags are stripped &#8230;</p> ]]></content:encoded> </item> <item><title>By: jQueryGlobe</title><link>http://davidwalsh.name/jquery-create-element#comment-12463</link> <dc:creator>jQueryGlobe</dc:creator> <pubDate>Mon, 05 Oct 2009 15:36:41 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=3869#comment-12463</guid> <description>Looks like reinventing the wheel. I don`t see why this method is better than writing:$(&#039;&#039;).attr(&#039;id&#039;, &#039;myid&#039;).css(&#039;opacity&#039;, &#039;0.5&#039;).appendTo(&quot;#wrap&quot;);</description> <content:encoded><![CDATA[<p>Looks like reinventing the wheel. I don`t see why this method is better than writing:</p><p>$(&#8221;).attr(&#8216;id&#8217;, &#8216;myid&#8217;).css(&#8216;opacity&#8217;, &#8217;0.5&#8242;).appendTo(&#8220;#wrap&#8221;);</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 2/7 queries in 0.014 seconds using disk: basic
Object Caching 805/805 objects using disk: basic

Served from: davidwalsh.name @ 2012-05-23 04:29:26 -->
