'How to get particular element with a className?
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
Solution 1:[1]
Use querySelectorAll
target.querySelectorAll('th.numerical')
I also suggest you read up on selectors
Solution 2:[2]
Well you can use querySelectorAll
target.querySelectorAll('th.numerical'))
This will give you all th elements with className of 'numerical'.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | TheRakeshPurohit |
| Solution 3 | Yves Kipondo |
