javascript - Two tables from one String variable of HTML -
an ajax query returns html string has 2 tables.
i want put table1 into div1 , table2 into div2
if html representing both tables (they're sequential, not nested or funny that) stored in variable twotables, how can use jquery selectors (or other method, although trying avoid direct string manipulation) split variable?
edit: data looks like
<table id="table1"> ... </table><table id="table2"> ... </table>
var $tables = $(twotables); $('#div1').append( $tables[0] ); $('#div2').append( $tables[1] );
example: http://jsfiddle.net/vhazv/
since twotables
represents html string of 2 sequential tables, send string jquery object, select each table dom element 0 based index.
or use .eq()
table wrapped in jquery object.
var $tables = $(twotables); $tables.eq(0).appendto('#div1'); $tables.eq(1).appendto('#div2');
here's no jquery version still uses 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 no-jquery in dom insertion.
Comments
Post a Comment