Skip to content Skip to sidebar Skip to footer

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 variabl

Solution 1:

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();

Solution 2:

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());

Solution 3:

You can even use native JS to do this. In this case, add the new HTML to a hidden div by using its innerHTML property like this:

document.getElementById('hidden_div_id').innerHTML = myHTML;

Once the new HTML is added, you can walk through nodes using whatever methods you want.

Solution 4:

Just inject it into a hidden div and manipulate what you need from it in there.

var myHTML;//variable with html from phpvar$hiddenDIV = $('<div></div>').hide().appendTo('body').html(myHTML);
/*Now you can traverse the contents of $hiddenDIV.
 * If you need to get the contents back:
 */var newHTML = $hiddenDIV.html();

Solution 5:

Yes. And even if that is not available you could make an invisible div and then parse it there.

Post a Comment for "Can Jquery Parse Html Stored In A Variable?"