tags:

views:

41

answers:

2

Hi, I need to take data from external file and format it, the problem is, i want to have that data stored into a variable. Load method works for me, because i need to load not the whole document, but just a part of it, but then again, i want to load to variable and not to some dom element. $.get can do this, but it doesn't support selectors. Is there any way I could make this? Now examples: Mine external file consists of a table, which has format like this:

<table><tr><td><img /></td></tr><tr><td><a></a></td><td><span></span></td></tr></table>

I need to extract img, a and span tags because I need them to be displayed in different order than they're now. So, is there any chance for me that i could make this work? Thanks.

+3  A: 

You have to do a get request and set result type to XML and then use jquery selectors to find stuff in the data.

examples here: http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

naugtur
+1 - I should have paid closer attention.
patrick dw
Thank you, I didn't know that I could do that with returned data, actually I didn't need to use xml type.This worked for me: $.get("url.php", function(data){ var img = $(data).find("img").attr("src"); }
bah
+1  A: 

EDIT: I didn't look closely enough at naugtur's answer. It is essentially what he was saying.


It should work to use $.get(). Not sure what you mean when you say it doesn't support selectors. You can use a callback, and work with the data returned.

$.get('/path/to/data', function(data) {
  // returned value is stored in 'data' variable
  // You can manipulate it, and append where you want
  $myImage = $('img', data);

  $myImage.appendTo('body');
});
patrick dw
By selectors, I mean that i can do this with .load("url.php img"), but that doesn't work with $.get("url.php img"). Or am I doing it wrong?
bah
@bah - Instead of accessing the `img` directly in the `$.get()` call, you would use a callback function like in my example, and then place the data returned in a jQuery object, and manipulate it like normal.
patrick dw
Yup. That's what my answer says. ;)
naugtur
@naugtur - Sorry. I think I saw XML and just tuned the rest out.
patrick dw
@partick - Don't worry, answering a new question is always such a rush. :)
naugtur