Iam trying to get td values in tr with id=jf and push there values into an array but when to get this array it just store the first td valuse and others array elements are Undefined
<table id="customers" >
<tr id="jf">
<td>Students Name</td>
<td>Students SSN</td>
<td>Class</td>
<td>First</td>
<td>Second</td>
<td>third</td>
<td>Final</td>
</tr>
</table>
<button id='addr' onclick='addnewr()'> Add </button>
<script>
var count_td_def=document.getElementById("jf").cells;
function insertdata()
{
var tdvs=[];
for(var j=0;j<count_td_def.length;j++)
{
var tdv=document.getElementById("jf").cells[j].innerHTML;
tdvs.push(tdv[j]);
alert(tdvs[j])
}
}
</script>
You can use document.getElementById
to get the row and querySelectorAll('td')
to get the td
. Then use map
to create an array of the text
function insertdata() { var tdv = [...document.getElementById("jf").querySelectorAll('td')].map((item) => { return item.innerHTML }) console.log(tdv) }
td { border: 1px solid black; }
<table id="customers"> <tr id="jf"> <td>Students Name</td> <td>Students SSN</td> <td>Class</td> <td>First</td> <td>Second</td> <td>third</td> <td>Final</td> </tr> </table> <button id='addr' onclick='insertdata()'> Add </button>
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.