DaniKR
DaniKR

Reputation: 2448

How to load data into variable instead selector in jQuery?

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.

Upvotes: 1

Views: 3490

Answers (2)

Dhiraj
Dhiraj

Reputation: 33618

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.

Upvotes: 3

mdfreeland
mdfreeland

Reputation: 11

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.

Upvotes: 1

Related Questions