1

I am having some trouble to load data from url with load method in jQuery into variable instead selector.

So my code is this:

$('#test').load('http://url #searchedID');

So this would load some string from url where html selector have id = searchedID e.g. "This is string" and then it would display this string into html selector where id = test.

My question is: How to load data in my case (from: #searchedID) instead into selector where id = test into variable. Something like this:

var loadHereData = load('url, #searchedID');

But this is not corect!

Realy thanks for any advice.

6
  • so essentially you want to copy content from #searchID to another div or something ? Commented Mar 10, 2015 at 15:29
  • no, just put/get in variable e.g. "var load" so i can in this page do something else, like parse etc, basicly I do not whant to display it, just load it or get it... Commented Mar 10, 2015 at 15:30
  • 2
    use $.get as mentioned here stackoverflow.com/questions/9958282/… Commented Mar 10, 2015 at 15:34
  • hmm @DhirajBodicherla this is more likly, do you think this would work: $.get("url #searchedID", function( my_var ) {}, 'html'); Commented Mar 10, 2015 at 15:38
  • 1
    how about someting like this jsbin.com/jujotafamo/1/edit?html,js,output you load the page and search in the page what you want and store it in your variable ? Commented Mar 10, 2015 at 15:52

2 Answers 2

3

You can $.get the page and search for the required element and store it like this

var myContent;
$.get("http://jsbin.com/ficoniniwo/1.html", function(data) {
  var tempData = $('<output>').append($.parseHTML(data)).find('h1');
  myContent = tempData.html();
}, 'text');

Here is a Demo

The html received from 1.html is converted to a jquery object by $.parsehtml and then I'm appending it so that I can search in it.

Hope this helps.

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

4 Comments

i see a problem with my answer. Please hold on
why is returning as result [object Object]?
I think i messed up something. Sorry about that. I'm working on it now.
updated my answer. Please check now and let me know if it works. Thanks
1

The jQuery load method places the downloaded HTML into a matched element. If you want to store the contents of #searchedID in a variable, you can first create a variable containing an empty jQuery DOM element.

var loadHereData = $("<div id='test'></div>");
loadHereData.Load('url #searchedID');

The loadHereData variable will now contain a div holding the contents of the #searchedID element.

1 Comment

also here result of loadHereData is [object Object]

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.