Is it possible to find an element within a string of HTML rather than from the DOM? Example,
var html = "<div><span class='find-me'></span></div>"
$("html").find(".find-me").remove();
You are very close, wrap your variable in $()
var $div = $(html);
$div.find(".find-me").remove();
If you want to return to string can do something like:
var htmlString = $('<div>').append($div).html();
$(html) builds a dom structure (though not within the page's dom tree). I'm under the impression that the OP addresses a search in the lexical representation alone, without parsing.filter() if it is at the root levelval() to get it. Or use data() or attr() or addClass() etcIf HTML is a string, you could just use javascript to treat it a string use the replace function, something like:
var html = "<div><span class='find-me'></span></div>"
html = html.replace("find-me", "");
alert(html)
In this case, after the replace, html would be:
<div><span class=''></span></div>
Here's the Fiddle
replace(/find-me/g, '') you face the problem that the class names in text nodes of the html will be replaced too.It is, but it is error-prone and unreliable. Consider <div><span class='find-me'>Devious web designer writes class='find-me' inside a text segment</span></div> - admittedly contrived but imho illustrative.
You are looking for structure information in semi-structured content. Why would you want to make your life harder by searching in _un_structured content ?
You could parse the string with DOMParser();
var html = "<div><span class='find-me'></span></div>"
dom_doc = new DOMParser().parseFromString(html, "text/xml");
$(dom_doc).find('#foo').remove();
jQuery and native JS, which is fine, but this is probably not the idiomatic way to do this, considering the tools jQuery gives you shield you from these details.
"html"and by that matching the html element instead of using hishtmlvariable as argument for the jQuery function. Also there is no need to write a DOM parser as one can use the browser to parse the string...