Home > Software design >  How to get particular element with a className?
How to get particular element with a className?

Time:03-14

I am using target.getElementsByClassName('numerical')) to get the list of elements with classname of numerical . I want to further filter only th from the list. How can i do that?

0: th.ant-table-cell.numerical
1: th.ant-table-cell.numerical
2: td.ant-table-cell.numerical
3: td.ant-table-cell.numerical

CodePudding user response:

Use querySelectorAll

target.querySelectorAll('th.numerical')

I also suggest you read up on selectors

CodePudding user response:

Well you can use querySelectorAll

target.querySelectorAll('th.numerical'))

This will give you all th elements with className of 'numerical'.

CodePudding user response:

You can create an array from element you get with getElementsByClassName and filter them like this

Array.from(target.getElementsByClassName('numerical'))
    .filter((tag) => {
        return tag.tagName === "TH");
    });

Here I use Array.from function to convert the result of getElementsByClassName to Array as it is an instance of HTMLCollection which is not iterable.

  • Related