I have the following string
str ='<div id="example">Text</div><div id="test">Test</div>';
how can I get the example and test content with jQuery.
You need to convert the text to a jQuery object and then use standard traversing methods
str ='<div id="example">Text</div><div id="test">Test</div>';
var live_str = $('<div>',{html:str});
var example = live_str.find('#example').text();
// example variable now holds 'Text'
var test = live_str.find('#test').text();
// example variable now holds 'Test'
demo at http://jsfiddle.net/gaby/FJSm6/
As you see i set the string as the html of another element, because otherwise the divs would be at the top level and you would not be able to traverse them with .find()
..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With