views:

135

answers:

2

Hi all

I am reading in a complex chunk of HTML and I'd love to be able to walk this sub-tree of HTML in the same way I can the DOM.

Is there a way in Prototype to take a raw chunk of HTML (say from an AJAX call) and 'wrap it' in some way so that it becomes a Prototype element and thus allow me to search it using .up() .down() etc?

Or do I need to add it to the DOM first before I can manipulate it in this way?

Thanks in advance.

+1  A: 

You could create a container element, give it an ID and put your complex HTML in it:

$('mydiv').update(complexHTML);

then you should be able to work through it like a DOM element.

Pekka
As I mentioned I was thinking of this but trying to avoid it. The 'mydiv' you mention could be styled 'display:none;' and I could work it like this, but I was hoping to create an element I could search seperate from the DOM.
Ciaran Archer
Have you tried createElement without connecting it to the DOM? You could then extend it without an ID: $(createdElementVariable).update()
Pekka
+4  A: 
var myDOMStructure = (new Element('div')).update(yourHTML);
myDOMStructure.down().down().up(); /// etc...
J-P
Beautiful. Exactly what I needed. Thanks!
Ciaran Archer
Simple and effective, solved a similar problem for me!
Michael Robinson