[Javascript] document.createElement("TBODY")

Hassan Schroeder hassan at webtuitive.com
Tue Apr 17 10:36:12 CDT 2007


Michael Borchers wrote:

> Is there any other way to parse the response than innerHTML?

Using innerHTML is not "parsing" the response. At all. It's sticking
a lump of cr--, er, whatever, into your page, rather than explicitly
(and IMHO properly) inserting elements into the DOM.

For simplicity's sake, let's say this row is returned by an AJAX
request:

   <tr><td>now</td><td>is</td><td>the</td><td>time</td></tr>

:: and you want to insert it into a tbody with the id "tb".

/* using Prototype */
function append(row)
{
	var tr = document.createElement("tr");
	$('tb').appendChild(tr);
	var cells = row.firstChild.getElementsByTagName("td");
	$A(cells).each( function(cell){
		var text =
                    document.createTextNode(cell.firstChild.nodeValue);
		var td = document.createElement("td");
		td.appendChild(text);
		tr.appendChild(td);
		});
}
function fetch()
{
	new Ajax.Request("/tr.jsp", {
		onSuccess  : function(transport){
			append(transport.responseXML); },
		onFailure  : function(){ alert('failed'); }
		});	
}

At least, that's how I'd do it :-)

FWIW,
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com
                opinion: webtuitive.blogspot.com
				
                          dream.  code.





More information about the Javascript mailing list