views:

128

answers:

2

I am successfully posting a form via ajax, using the following code;

 $.post( "Page.do?source=ajax", 
            $("#Form").serialize(), 
            function(data){ 


                }

The data response that comes back can be alert'd. I can see that it is the HTML of the entire form, having been submitted. But, I am having trouble accessing an element in that form which has come via ajax, ie

data.find('.Errors').html() 

or

$('.Errors', data).html()

Do I need to somehow convert the text into a DOM which can then be parsed by jQuery?

+1  A: 

Do I need to somehow convert the text into a DOM which can then be parsed by jQuery?

Yes you do:

$(data).find('.Errors').html();

Although normally using data as the scope for the selector like you showed in your second example should work.

prodigitalson
+2  A: 

Correct, otherwise you would have to apply regex to the result (which is a string and not a DOM).

You can convert it to DOM by:

$(data)

and then applying any jQuery you want to it.

Bryan Denny
can you give an example Bryan? I can't get this to work
Andy
@Andy: The example @prodigitalson gave should work: $(data).find('.Errors').html(); assuming that your data is valid HTML.
Bryan Denny
hmm, removing elements from $(data) is more difficult but I've got it to work, thanks.
Andy