1

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?

GrahamB
  • 589
  • 1
  • 6
  • 19
  • first you need to convert it to a jQuery object, but even after that .find() might not work see http://stackoverflow.com/a/8612928/1404348 and http://stackoverflow.com/a/405700/1404348 – MTVS Feb 15 '13 at 17:54

2 Answers2

2

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
  • 27,363
  • 32
  • 109
  • 125
  • @Andy: The example @prodigitalson gave should work: $(data).find('.Errors').html(); assuming that your data is valid HTML. – Bryan Denny Mar 24 '10 at 18:30
  • hmm, removing elements from $(data) is more difficult but I've got it to work, thanks. – Andy Mar 25 '10 at 08:57
1

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
  • 60,050
  • 10
  • 100
  • 114
  • .find() might not work see http://stackoverflow.com/a/8612928/1404348 and http://stackoverflow.com/a/405700/1404348 – MTVS Feb 15 '13 at 17:55