views:

220

answers:

3

I'm using MooTools ( part of the project ) to load a page using Request.HTML which works fine except that I don't want the whole page, just one fragment which has an id.

This is the code in question

var req = new Request.HTML({
    onSuccess: function( res ) {
        // according to the docs
        // res should be the node list of the remote response
        // I want to grab #myFragment

        var f = res.getElementById('myFragment');
        // res.getElementById is not a function

        var f = $(res).getElementById('myFragment');
        // $(res) is null ?

        var f = $$(res).getElementById('myFragment');
        // [null, null] ??


        // more code

    }
}).get('/myurl');

I'm pretty sure this must be possible, I can grab elements that have a class. Does anyone know how to do this.

Thanks )

+1  A: 

Hi meouw,

I'm not familiar with Mootools but when digging into Request.HTML documentation found this:

Request success event: onSuccess(responseTree, responseElements, responseHTML, responseJavaScript)

and

responseElements - (array) An array containing all elements of the remote response.

Hope this would give right direction to solve your problem.

Darmen
Yep, I'm trying to use the responseTree in the question. The responseElements array isn't much use:- my element contains a lot of elements so finding the closing tag is impossible (it's a one dimensional array) - thanks tho
meouw
+1 Darmen, You were pulling the right thread
meouw
Glad to help, @meouw . Thanks for your vote
Darmen
A: 

The new jQuery 1.4 does that easily, so I recommend that if You can switch libs

$('#result').load('ajax/test.html #container');

http://api.jquery.com/load/

EDIT:

As for the Moo - I expect You could put the downloaded html in a hidden container and then search as usually, cut out what You need and dump the hidden container's insides. Mem-consuming, but should work.

naugtur
I know, so did the old jQuery as I remember. This is a MooTools question though unfortunately
meouw
+3  A: 

I hopped onto the #mootools channel on irc.freenode.net and got my answer from <kamicane> himself

var req = new Request.HTML({
    onSuccess: function( responseTree, responseElements /*more*/  ) {
        // responseElements is the one I want
        //it's an array of elements which you can filter
        var f = responseElements.filter('#myFragment');

        // do stuff with my fragment

    }
}).get('/myurl');
meouw