views:

46

answers:

2

Result (data) looks like this:

<tr>
   <td>
      Something...
   </td>
</tr>

<div id="paging">1, 2, 3... </div>

This is ajax

...
dataType: "html",
success: function(data) {
    parse data...    
    $('#myDiv1').html(data1);
    $('#myDiv2').html(data2);
}
...

Is it possible to parse data so that data1 contains table row(s) and data2 contains div#paging content?

Thanks in advance,
Ilija

+2  A: 

try..

var data1 = $(data).find('tr');
var data2 = $(data).find('div#paging');

edit:

as Guffa, mentioned in below comments, you cannot parse it if the html is broken in structure... but I suspected you got more than that codes... anyway, here's a demo

Reigel
when I alert data1 or data2 I only see [object Object]
ile
@ile that's not wrong. did you try `html()`ing them in?
Agos
if you want to try to alert, do for example `alert(data1.html());`
Reigel
That doesn't work as the HTML is not complete.
Guffa
@Guffa - what do you mean by doesn't work?... isn't this ajax result?
Reigel
@Reigel: There is no `table` tag around the td, so the code won't be parsed correctly.
Guffa
@Guffa - ahhh I assumed he got more codes than that... okay I will update my answer... thanks...
Reigel
it works with div element but doesn't with tr
ile
@ile - please take note that there are many li... so you might need to iterate each...
Reigel
I see... obviously I need to have correct syntax so that elements can be selected.Thanks!
ile
A: 

As the HTML code is not complete, it can't simply be parsed by the browser. You have to parse it manually.

For example:

var match = /(<tr>.+</tr>)\s*(<div.+</div>)/.exec(data)
var data1 = match[1];
var data2 = match[2];
Guffa
-1 for parsing html with a regex, while ignoring the selector engine.
Agos
@Agos: As I said in the answer, that **doesn't work**. As the HTML is not complete, you **can't** parse it that way, and thus can **not** use the selector engine.
Guffa