0

An AJAX query returns an HTML string that has 2 tables.

I want to put table1 into div1 and table2 into div2

If the HTML representing both tables (they're sequential, not nested or anything funny like that) is stored in variable twoTables, how can I use jQuery selectors (or any other method, although I am trying to avoid direct string manipulation) to split up the variable?

edit: data looks like

<table id="table1"> ... </table><table id="table2"> ... </table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sova
  • 5,468
  • 10
  • 40
  • 48
  • Can you add an example of returned data? – wajiw Nov 15 '10 at 21:51
  • Can you give an example of the HTML you get back from your ajax call? Is it similar to "..
    ...
    "?
    – Peter Jacoby Nov 15 '10 at 21:51
  • Any chance you can change it from returning a single string to returning a JSON object? – Cᴏʀʏ Nov 15 '10 at 21:52
  • If you are only able to retrieve a string variable then I think the only way to process the html is through direct string manipulation. – amaseuk Nov 15 '10 at 21:53
  • You might like to follow advice for returning different representation, such as JSON. – amaseuk Nov 15 '10 at 21:54
  • @top: the returned data is like ...
    ...
    @bottom: Now that you mention it, JSON sounds like the way to go...
    – sova Nov 15 '10 at 21:59
  • @aaronmase - Why JSON, when browsers have very fast native HTML parsers built in? It is going to be parsed as HTML eventually, so you might as well just do that instead of parsing as JSON *and* HTML. – user113716 Nov 15 '10 at 22:05
  • @patrick agreed, I was just referring to the comment above, which is a viable solution, but I do agree that parsing html would probably be more efficient. – amaseuk Nov 16 '10 at 09:55

5 Answers5

2

You can split the two tables into an array. Check out the following:

var s = "<table>Dude!</table><table>What?</table>";
var a = s.match(/<table>.*?<\/table>/gi);
alert(a);

So table one will be in a[0], and table two in a[1].

Brendan
  • 1,853
  • 11
  • 18
  • Consider [this answer](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) regarding HTML parsing via regex. – user113716 Nov 15 '10 at 22:01
  • @patrick dw: Sova is getting a string from a server through XHR. Assuming he has control over the server response, it should simply be considered a string that is as predictable as he sees fit, rather than some unparsable demonic HTML document. – Brendan Nov 15 '10 at 22:15
  • Yes, but that is a great answer, right? ;o) Anyway, your answer contemplates a very simple `` tag. But in actuality, you'll need to make it contemplate possible attributes that may be set on those opening tags. I'd really suggest not using regex, when browsers have extremely fast HTML parsers built in that are going to have to run anyway when the HTML is appended to the DOM. :o)
    – user113716 Nov 15 '10 at 22:19
  • @patrick dw: Yeah, that is a sweet answer! :D And you're right, that regex doesn't take enough into consideration. – Brendan Nov 16 '10 at 14:34
1
var $tables = $(twoTables);

$('#div1').append( $tables[0] );
$('#div2').append( $tables[1] );

Example: http://jsfiddle.net/VhAzV/

Since twoTables represents an HTML string of 2 sequential tables, just send the string to a jQuery object, then select each table DOM element by its zero based index.

Or you could use .eq() to get the table wrapped in a jQuery object.

var $tables = $(twoTables);

$tables.eq(0).appendTo('#div1');
$tables.eq(1).appendTo('#div2');

Here's a no jQuery version that still uses the browser's native HTML parser:

Example: http://jsfiddle.net/patrick_dw/VhAzV/2/

var twoTables = '<table><tr><td>table one</td></tr></table><table><tr><td>table two</td></tr></table>';
var $tables = document.createElement('div');
$tables.innerHTML = twoTables;

document.getElementById('div1').appendChild($tables.firstChild);
document.getElementById('div2').appendChild($tables.firstChild);

EDIT: Made it truly no-jQuery in the DOM insertion.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • This is awesome to know! Is there a list of which elements make it into DOM references? Obviously a line break or strong tag wouldn't be reference-able via the DOM element indicies, right? – sova Nov 23 '10 at 04:16
  • @sova - Actually, pretty much any tag/text is converted into a DOM element (even code comments). Because the DOM is the object representation of the page elements, everything needs to have some representation. So even if you inserted some text between the two tables, a text node would be created that could be referenced. There are some minor differences between some browsers when it comes to empty space between tags or corrections that are made for invalid markup. – user113716 Nov 23 '10 at 04:45
0

As per http://api.jquery.com/load/

$('#result').load('ajax/test.html #container');

You can do this twice for two tables.

Mikhail
  • 8,692
  • 8
  • 56
  • 82
0

If you have a string that looks something like this:

var foo = "<table><tr><td>1</td></tr></table><table><tr><td>2</td></tr></table>";

First make it a jQuery object:

foo = $(foo);

Then you can get each table and insert them wherever you'd like:

$("#div1").append(foo[0]); // First table
$("#div2").append(foo[1]); // Second table
Matt
  • 61
  • 6
0

This might be ugly but it's the first thing that comes to my mind, hopefully others will find a more elegant solution:

var tableEnd = '</table>';

var arr = twoTables.split(tableEnd);

var t1 = arr[0].concat(tableEnd), t2 = arr[1].concat(tableEnd);

div1.innerHTML = t1;

div2.innerHTML = t2;

bonus: no jQuery overhead! :)

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • Using the ending tag is an interesting solution as long as we would know that it'll look exactly like that. One thing to consider though would be the possibility of nested tables. Not sure if that's the case here, but it would throw a pretty big wrench into the operation if so. :o) – user113716 Nov 16 '10 at 14:50
  • the OP said that there were no nested tables. Also this is faster and does not need jQuery at all – Pablo Fernandez Nov 16 '10 at 17:24
  • Well, the tables to be separated weren't nested. But I'd grant that in this situation there probably aren't any others nested. With regard to performance, it depends, though I'd agree that jQuery would slow it down. Using native API, it [appears to be faster](http://jsperf.com/table-via-dom-parsing-vs-regex) to use the HTML parser, given the assumption that OP intends to ultimately turn the tables into DOM elements. – user113716 Nov 20 '10 at 16:25
  • YAGNI (and some more chars to reach the permitted length for a comment) – Pablo Fernandez Nov 20 '10 at 19:23