How to transform in the DOM following dl dt values to table on the fly using jQuery or d3js in a document Rows may have any order of data elements and some missing columns too... It feels like it requires transpose operation rows to columns
From
<div id="conatainer">
<dl class="table">
<dt class="row">1</dt>
<dd>
<dl>
<dt>name</dt>
<dd>Tom Cruise</dd>
<dt>age</dt>
<dd>56</dd>
</dl>
</dd>
<dt class="row">2</dt>
<dd>
<dl>
<dt>dob</dt>
<dd>April 4, 1965</dd>
<dt>name</dt>
<dd>Robert Downey Jr.</dd>
<dt>age</dt>
<dd>53</dd>
</dl>
</dd>
</dl>
</div>
To
<div id="conatainer">
<table>
<thead>
<tr>
<th>s.no</th>
<th>name</th>
<th>age</th>
<th>dob</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tom Cruise</td>
<td>56</td>
<td> </td>
</tr>
<tr>
<td>2</td>
<td>Robert Downey Jr.</td>
<td>53</td>
<td>April 4, 1965</td>
</tr>
</tbody>
</table>
</div>
Set an ID for the table and hide it initially, then use replaceWith
$('button').click(() => { const newTable = $('#new-table'); newTable.show(); $('dl').replaceWith(newTable); });
dl{ background: green } table{ background: blue; display: none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>replace</button> <dl class="table"> <dt class="row">1</dt> <dd> <dl> <dt>name</dt> <dd>Tom Cruise</dd> <dt>age</dt> <dd>56</dd> </dl> </dd> <dt class="row">2</dt> <dd> <dl> <dt>dob</dt> <dd>April 4, 1965</dd> <dt>name</dt> <dd>Robert Downey Jr.</dd> <dt>age</dt> <dd>53</dd> </dl> </dd> </dl> <table id='new-table'> <thead> <tr> <th>s.no</th> <th>name</th> <th>age</th> <th>dob</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Tom Cruise</td> <td>56</td> <td></td> </tr> <tr> <td>2</td> <td>Robert Downey Jr.</td> <td>53</td> <td>April 4, 1965</td> </tr> </tbody> </table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.