Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling jQuery.remove() on a DOM made from a string

html = '<h1>foo</h1><p>bar</p>';
virtual_dom = $(html);
console.log(virtual_dom);
// logs a data structure recognizable as a DOM with the h1 and p from the string

Does jQuery provide a way to remove paragraphs from virtual_dom, such that console.log(virtual_dom) will log a DOM with only a h1 tag, like $('p').remove() but affecting virtual_dom instead of the actual document rendered by the browser?

like image 695
Jordan Avatar asked Dec 28 '25 16:12

Jordan


1 Answers

Use .filter(), for example:

virtual_dom = $(html).filter(":not(p)");

This will exclude all paragraphs from your html.

DEMO.

like image 143
João Silva Avatar answered Dec 30 '25 06:12

João Silva