Created
June 25, 2009 21:30
-
-
Save jacobo/136179 to your computer and use it in GitHub Desktop.
jQuery should have build function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//define this: | |
jQuery.fn.build = function(htmlElem, attributes){ | |
if(!attributes){ attributes = ""; } | |
this.append("<"+htmlElem+" "+attributes+"></"+htmlElem+">"); | |
return this.children(":last"); | |
} | |
//and then you can do things like this: | |
$("#mydiv"). | |
build("table"). | |
build("tbody"). | |
build("tr"). | |
build("td", "class='axis_label'"). | |
build("label"). | |
text("Hi"); | |
//Which would of course alter the contents of mydiv to be this: | |
<table> | |
<tbody> | |
<tr> | |
<td class="axis_label"> | |
<label>Hi</label> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
//which feels cleaner than the alternative string-appending or template approaches | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment