It doesn't work because find()
searches the descendants and your paragraph must be at the top level of the HTML you're searching.
For example:
alert($("<p id='one'>one</p><p id='two'>two</p>").find("p:first").attr("id"));
returns "undefined" whereas:
alert($("<p id='one'>one</p><p id='two'>two</p>").filter("p:first").attr("id"));
will output "one".
So you could use filter()
if you know it's at the top level (possibly falling back to find()
). Alternatively you could wrap the whole lot up in a dummy element:
alert($("<div>" + html + "</div>").find("p:first").text());
Edit: My advice? Use:
newtitle = $(newtitle).filter("p:first").text();