Skip to content

Instantly share code, notes, and snippets.

@jacobo
Created June 25, 2009 21:30
Show Gist options
  • Save jacobo/136179 to your computer and use it in GitHub Desktop.
Save jacobo/136179 to your computer and use it in GitHub Desktop.
jQuery should have build function
//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