Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery Parse HTML Stored in a Variable?

I'm using PHP and an ajax command to take the entire HTML contents of an external web page (via the PHP file_get_contents() command) and passing that HTML into a javascript variable. Once I have the page's HTML contents stored in a variable, can I use jQuery to interact with the contents of that variable, in the same way that jQuery normally interacts with the DOM? In this example, I am trying to search for the existence of certain HTML elements (<div> and <script> tags) with specific ID attributes. Can anyone suggest how I can accomplish this?

like image 548
jake Avatar asked May 10 '11 14:05

jake


Video Answer


2 Answers

If I understand you correctly, you should be able to just pass the variable to the jQuery function and work accordingly.

A quick example with .filter():

$(myHtml).filter('#someid').doStuff();
like image 74
BoltClock Avatar answered Sep 30 '22 04:09

BoltClock


Just pass it as a string to the jQuery constructor.

var foo = jQuery('<p><b>asd</b><i>test</i></p>').
alert(foo.find('i').text());
like image 40
Quentin Avatar answered Sep 30 '22 03:09

Quentin