Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Javascript access the DOM of an Ajax text/html response?

I'm trying to use Ajax to fetch an HTML page and then pull out a div by it's ID, and insert that DIV into the current page. So the current page loads (via Ajax) a second page, pulls the div out of the Ajax response and inserts into the current page. However I am at a loss as unless the response is text/xml, I cannot use any DOM functions on it... can I?

like image 529
Josh Avatar asked May 12 '09 23:05

Josh


1 Answers

Jquery (or other libraries??) will basically do all this for you. I highly recommend looking into this and not reinventing the wheel.

For query it would probably like:

// Make a call to url, with data 
$.ajax(
  { url: $url,
    data: $data, 
    dataType: 'xml',
    callback: function(returnData) { 
      // Jquery find <mytag attribute="foo">...</mytag> and store it in mydata
      var mydata = $(returnData).find('mytag[attribute=foo]');
      // Insert into current page to somewhere with class="after-me"
      $('.after-me').html(mydata);
   }
});

I may have the syntax wrong. But here is the docs: http://docs.jquery.com/Ajax/jQuery.ajax#options

like image 103
jon skulski Avatar answered Oct 28 '22 14:10

jon skulski