Is there anyway to target all of <th> in a table and declare same attribute for all of them?

i have this table with crazy amount of data,i was wondering isn’t there any way to access all <th>elements and set an attribute which will be apllied to all of them?
for instance-

<tr>
    <th colspan="7"> title </th>
 </tr>

i just wanna declare them in the beginning so that i don’t have to set each one of them, when i have a massive amount of data! :S

Yes this is very elementary to do via JavaScript: Element.setAttribute() - Web APIs | MDN

It’d be better if you assigned a class, but as long as you don’t have any other “th” elements to get in the way, you could target them on their own too.

1 Like

you can use queySelecorAll() to select all the th, you get an html collection (array-like data structure) over which you can loop, and add the attribute to each element

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

1 Like

Are you trying to do this with JavaScript or with CSS?

In either case, it is possible.

i was wondering if i could do it in HTML level. Guess,this is beyond HTML’s scope.Poor HTML. lol

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.