0

This works in Chrome and FF, but not Safari :/

var content = '<div><span><p>Can you catch me?</p></span></div>';
content = $.parseXML(content);
var span = $(content).find('span').html();

I need the output as a string. And I would prefer nothing to be put into the DOM.

In Chrome and FireFox it outputs "<p>Can you catch me?</p>" Which is what I want.

If you dump this JS into the Safari console, you get a document output, but its the whole thing, not just the span.

dezman
  • 18,087
  • 10
  • 53
  • 91

2 Answers2

2

I think you want to do something like this:

var $content = $('<div><span><p>Can you catch me?</p></span></div>');
var $span = $content.find('span');

now span will be the jquery wrapped html <span><p>can you catch me?</p></span>.

If you want the <p> element you would do:

var $p = $content.find('p');
thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
0

More reliable - use document:

<div id="temp" style="display: none;"></div>

and

$("#temp").html("<div><span>...");
var span = $("span", "#temp").html();
$("#temp").html("");