Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery parseHTML - Getting and Setting Values

Tags:

html

jquery

Is it possible to use jQuery to read/parse a selection of valid HTML such that you can use jQuery methods on it such as .val() or .html()?

The below is not working for me. I have tried .html() and .val().

html = $.parseHTML('<div id="one">blah</div><div id="two">blurp</div>');
console.log ($(html).find('#one').text());
like image 625
Ronnie Royston Avatar asked Oct 24 '25 05:10

Ronnie Royston


1 Answers

No need to use parseHTML on HTML string.

Wrap it in jQuery object and use methods on it.

var html = $('<div id="one">blah</div><div id="two">blurp</div>').filter('#one').html();
document.write(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
like image 69
Tushar Avatar answered Oct 26 '25 18:10

Tushar