i have variable in javascript value html table below.
<table border="1" cellpadding="2" cellspacing="2" dir="ltr" height="76" width="500"> <tbody> <tr> <td style="width: 166px;"> <strong>name</strong> </td> <td style="width: 166px;"> <strong>date</strong> </td> <td style="width: 168px;"> <strong>request type</strong> </td> </tr> <tr> <td style="width: 166px;"> name1 </td> <td style="width: 166px;"> 11th may 2015 </td> <td style="width: 168px;"> test </td> </tr> <tr> <td style="width: 166px;"> name2 </td> <td style="width: 166px;"> 12th may 2015 </td> <td style="width: 168px;"> test </td> </tr> <tr> <td style="width: 166px;"> name3 </td> <td style="width: 166px;"> 28th may 2015 </td> <td style="width: 168px;"> test </td> </tr> </tbody> </table>
now need parse through table , convert json array. suggestions how can achieved.
$(document).ready(function(){ var htmltable ='<table border="1" cellpadding="2" cellspacing="2" dir="ltr" height="76" width="500"> <tbody> <tr> <td style="width: 166px;"> <strong>name</strong> </td> <td style="width: 166px;"> <strong>date</strong> </td> <td style="width: 168px;"> <strong>request type</strong> </td> </tr> <tr> <td style="width: 166px;"> name1 </td> <td style="width: 166px;"> 11th may 2015 </td> <td style="width: 168px;"> test </td> </tr> <tr> <td style="width: 166px;"> name2 </td> <td style="width: 166px;"> 12th may 2015 </td> <td style="width: 168px;"> test </td> </tr> <tr> <td style="width: 166px;"> name3 </td> <td style="width: 166px;"> 28th may 2015 </td> <td style="width: 168px;"> test </td> </tr> </tbody> </table>'; htmltable = $(htmltable); var tablejsonobj = []; htmltable.find("tbody tr").slice(1).each(function(){ var name = $(this).find("td:eq(0)").text(); var date = $(this).find("td:eq(1)").text(); var type = $(this).find("td:eq(2)").text(); var obj = {name, date, type}; tablejsonobj.push(obj); }); console.log(tablejsonobj); });
Comments
Post a Comment