Hello I have table like this
<table>
<tbody>
<tr>
<th>PROVINSI</th>
<th>KABKOT</th>
<th>KECAMATAN</th>
<th>DESA</th>
</tr>
<tr>
<td>KALIMANTAN TENGAH</td>
<td>SERUYAN</td>
<td>SERUYAN HILIR</td>
<td>TANJUNG RANGAS</td>
</tr>
</tbody>
</table>
I want to convert this table into unordered list like this
<ul>
<li>PROVINSI<p>KALIMANTAN TENGAH</p></li>
<li>KABKOT<p>SERUYAN</p></li>
<li>KECAMATAN<p>SERUYAN HILIR</p></li>
<li>DESA<p>TANJUNG RANGAS</p></li>
</ul>
how can i do this with javascript?
i've tried this
function(){
var ul = $("<ul>");
$("table tr").each(function(){
var li = $("<li>")
$("th, td", this).each(function(){
var p = $("<p>").html(this.innerHTML);
li.append(p);
});
ul.append(li);
})
$("table").replaceWith(ul);
}
but i don't really understand how to loop over this table. sorry
Assuming that <table id="target">...</table>
JSFiddle
var ul = $("<ul>");
$("#target th").each(function(i, v){
var li = $("<li>")
li.append($(v).text());
var p = $('<p>').append($($("#target td")[i]).text());
li.append(p);
ul.append(li);
})
$("#target").replaceWith(ul);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With