0

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

when I alert data1 or data2 I only see [object Object]
if you want to try to alert, do for example alert(data1.html());
@Guffa - what do you mean by doesn't work?... isn't this ajax result?
@Reigel: There is no table tag around the td, so the code won't be parsed correctly.
@Guffa - ahhh I assumed he got more codes than that... okay I will update my answer... thanks...
|
-1

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];

2 Comments

-1 for parsing html with a regex, while ignoring the selector engine.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.