With this table,
<table >
<colgroup>
<col class="classname1">
...
</colgroup>
<thead>
<tr>
<th class="classname2" scope="col">title</th>
...
</tr>
</thead>
<tbody>
I can get the class name specified explicitly on the table header (th) by calling
th = document.querySelector...
th.classList // contains classname2 but not classname1
Given th, I want to obtain all classnames that apply. How can I get the class names specified in the corresponding colgroup?
3
Answers
You can assign an ‘id’ to the colgroup you want, then use:
const colGroup = document.getElementById("yourId")
const yourClasses = colGroup.classList
.It will return an array with all the element classes.
An approach was to retrieve …
within a first step the column specific class-names (as array of class-name arrays) while also taking a col-elements
span
-property into account.and within a final step, while iterating the direct child-elements of a specific table-row, to create the array of each child-element’s effective class-names array by merging the current element’s class-names with the class-names of its related col-element (this time taking into account a child-element’s
colSpan
-property).If I understand you correctly you can search for the index of your targeted
<th>
inside it’s parent<tr>
.Then loop over each
<col>
and keep track of the (number +span
). If that exceeds the index found before, you have the correct<col>