jQuery - selectors on a html string

Posted by ExiaHuang on April 11, 2017

jQuery - selectors on a html string

use `jQuery.parseHTML` to parse the HTML into an array of elements; then you’ll be able to convert it to a jQuery collection and use `filter` to restrict the collection to elements matching a selector.
1
2
3
4
5
6
7
8
9
10
11
var html =
    '<td class="test">asd</td>' +
    '<!-- -->' +
    '<td class="second">fgh</td>' +
    '<td class="last">jkl</td>';

var obj = $($.parseHTML(html)).filter('.test');
console.log(obj);

var text = $($.parseHTML(html)).filter('.test').text();
console.log(text);

jquery parseHTML not working as expected

My guess is that your data.description is escaped and the method parseHTML isn't going to handle that. Check out this post for a solution:

Javascript decoding html entities

1
2
3
var text = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';
var decoded = $('<div/>').html(text).text();
console.log(decoded);

So in your case:

1
2
var decoded = $('<div/>').html(data.description).text();
$('#gbFullPic').html(decoded);

jQuery - selectors on a html string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var url = 'https://developer.salesforce.com/docs/get_document_content/api_tooling/tooling_api_objects_apexcodecoverageaggregate.htm/en-us/206.0';
$.get(url, function(result){
     console.log('>>>result');
     console.log(result);
     console.log(result.id);
     console.log(result.title);
     console.log('>>>>result.content');
     // console.log($.parseHTML(result.content.toString()));
     var decoded = $('<div/>').html(result.content);
     // console.log(decoded);
     // console.log(decoded.text());
     // console.log(decoded.html());

     var html = $(decoded.html()).find('td > span.keyword');
     html.each(function(){
        console.log($(this).text());
     });
});