6 ms·
No sense to include another library just to change the syntax a little bit $("<p>",{"class":"gretting",html:"Hellow World"}).appendTo("#helloWorld .output"
by jQueryIsAwesome 14y ago
No sense to include another library just to change the syntax a little bit
$("<p>",{"class":"gretting",html:"Hellow World"}).appendTo("#helloWorld .output")
And you can also create hierarchy
$("<p>",{"class":"gretting",html:
$("<a>",{href:"#",html:
$("<h1>",{html:"Hello World"})
})
}).appendTo("body")
Which looks disgusting but that is why template engines exist.
- dyscrete 14y agoYou took the words right out of my mouth.
- solox3 14y agoIndeed. jQuery is awesome.
- eranation 14y agoThis made me wonder if there is a jQuery library that supports zen code, apparently there is: https://github.com/zodoz/jquery-ZenCoding https://github.com/zodoz/jquery-ZenCoding
- deleted 14y ago[deleted]
- jamesrom 14y agoFurther more, what's wrong with chaining function calls? It makes the whole thing a lot easier to read. $("<p>") .addClass("greeting") .html("Hello World") .appendTo("#helloWorld .output") And your second example: $("<p>") .addClass("greeting") .append( $("<span>") .html("Hello ") .append( $("<a>") .prop("href", "google.com") .html("World") ) ) .appendTo("body"); It's just as verbose but much more readable.
- chime 14y agoSpeed might be a godo reason for the more compact version, especially if you're doing this in a loop.