DOM templating with JavaScript?

I've been doing more and more JavaScript programming. I've run into a situation where I want to create a fragment then clone it repeatedly, changing a couple of ids and node values (picture a form with maybe ten fields, I want to update the value of a couple of fields and the id of a couple of fields and then add the result to the DOM).

It feels like overkill to walk the tree for each change. I kind of want to do a sprintf(). I am wondering if I can fetch the element by the technically duplicate id and change the node value and id to something else, before appending it to the tree with the original node (where the duplicate id could conflict).

Posted in Programming

Comments

Comments are hosted through disqus effective November 2008.

From: MikeD
Date: April 21, 2007 07:11 AM

For this sort of work, I use regular expression search and replace against text templates. I haven't tried doing that with a live DOM fragment, so text based templates may not be as performant as you need. The template formats I usually use curly-brace style tokens for the search and replace, but of course anything would work.

202: Accepted Archives

Feed icon We use Feedburner to distribute our web feeds: 202 Accepted Feed

feedburner graphic

Comments are hosted via disqus.

Google

Copyright 2002–2008 Artific Consulting LLC.

Unless otherwise noted, content is licensed for reuse under the Creative Commons Attribution-ShareAlike 3.0 License. Please read and understand the license before repurposing content from this site.