2

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.

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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"); }
1

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');
});

5 Comments

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 - 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.
Yup. That's what my answer says. ;)
@naugtur - Sorry. I think I saw XML and just tuned the rest out.
@partick - Don't worry, answering a new question is always such a rush. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.