Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / jQuery - Parse returned 'data' as HTML for further selecting?

a webservice returns some data to me. The data is actually just raw HTML (so no XML header, or tags around it, but just a piece of html).

<div class="Workorders">
    <div id="woo_9142" class="Workorder">
        <span class="Workorder">S1005</span>
        <span class="Pn">30-2</span>
        <span class="Description">Cooling Fan</span>
        <span class="Shortages">3616-1 (SV)</span>
        <span class="Company">xxx</span>
    </div>
    <div id="woo_9143" class="Workorder">
        <span class="Workorder">S1006</span>
        <span class="Pn">30-2</span>
        <span class="Description">Cooling Fan</span>
        <span class="Shortages">3616-1 (SV)</span>
        <span class="Company">xxx</span>
    </div>
</div>

If this were XML like so:

<workorders>
    <workorder id="woo_9142">
        <partnumber>30-2</partnumber>
    </workorder>
</workorders>

I could go like this in jQuery:

$('/workorders/workorder', data).each(function() {
    //This would give every partnumber $('partnumber', this).text();
});

How can I parse the returned HTML (like described at the beginning)?

myNamespace.onSuccess = function(request) {
    //request contains the raw html string returned from the server

    //How can I make this possible:
    $(request).find('div.Workorders div.Workorder').each(function() {
       //Do something with the Workorder DIV in 'this'
    });
}
like image 657
Ropstah Avatar asked Mar 01 '23 14:03

Ropstah


2 Answers

something like

myNamespace.onSuccess = function(request) {    
    $(request.responseText).filter('div.Workorder').each(function() {
       $('span.Pn', $(this)).text();
    });
}
like image 148
Russ Cam Avatar answered Mar 05 '23 22:03

Russ Cam


Have you tried adding the html to the dom, hiding it and then process it:

myNamespace.onSuccess = function(request) {
    var hidden = document.createElement ( 'div' );
    hidden.id = 'hiddenel';
    $("body").append ( hidden );
    $("#hiddenel").css ( 'visibility', 'hidden' );
    $("#hiddenel").html ( resp );
    $("#hiddenel").find ( 'div.Workorders div.Workorder').each(function() {
    .....
    });
}
like image 26
Martin Redmond Avatar answered Mar 05 '23 20:03

Martin Redmond