Home > Software engineering >  How to count number of divs in a td
How to count number of divs in a td

Time:10-14

I want to count the number of divs in a td of a table and store it in a variable.

Image of the html: https://imgur.com/a/T476Tkx

Or here is an example of the html:

<td colspan="1"  xpath="1">

<div  style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div ><span> 7:00 am</span></div></div>

<div  style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div ><span> 8:00 am</span></div></div>

<div  style="height: 133px; background-color: rgb(167, 211, 255); width: 145px;"><div ><span> 9:00 am</span></div></div>

</td>

How can I do this?

CodePudding user response:

Try this :

const arr = document.getElementsByTagName('td')[0].children;
const totalDivCount = Object.values(arr).filter(d => d.nodeName == "DIV").length

totalDivCount holds the value of the number of div present in td element.

You can also add ID to td and use it to get the element easyly.

CodePudding user response:

you can use xpaths to match:

count(//td[@]/div)
  • Related