Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  134] [ 4]  / answers: 1 / hits: 17252  / 9 Years ago, mon, may 4, 2015, 12:00:00
var html = '<p>sup</p>'


I want to run document.querySelectorAll('p') on that text without inserting it into the dom.



In jQuery you can do $(html).find('p')



If it's not possible, what's the cleanest way to to do a temporary insert making sure it doesn't interfere with anything. then only query that element. then remove it.



(I'm doing ajax requests and trying to parse the returned html)


More From » dom

 Answers
25

With IE 10 and above, you can use the DOM Parser object to parse DOM directly from HTML.



var parser = new DOMParser();
var doc = parser.parseFromString(html, text/html);
var paragraphs = doc.querySelectorAll('p');

[#66750] Saturday, May 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aliah

Total Points: 118
Total Questions: 132
Total Answers: 94

Location: Tajikistan
Member since Fri, Sep 11, 2020
4 Years ago
;