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

I think it might be possible to target the element by using the querySelector() method from the document object.

<script>
   const ths = document.querySelectorAll("th");
   ths.forEach(th => th.setAttritube("colspan", "7");
</script>

Let me know if it works. :slight_smile:

1 Like

Hey,yes.this is how it is done with JS.I was wondering if there any built-in HTML way of doing this.no luck.haha. Thanks man!

1 Like