user310291
user310291

Reputation: 38210

Why can't I set colspan for TH tag programmatically?

I have a table with thead with first th colspan = 1. I want to set it programmatically to value 2 : it doesn't work, how to do this ?

Here's source code :

https://jsfiddle.net/Lgof8m6q/

<table>
    <thead>
        <tr>
        <th scope="col" colspan="1">TITLE</th>
        </tr>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>

            <td></td>    


        </tr>
    </tbody>
</table>

javascript :

    var myTable = document.getElementsByTagName('table')[0];
    var myThead = myTable.getElementsByTagName('thead')[0]; 
    _tr = myThead.getElementsByTagName("TR")[0];
    _th = _tr.getElementsByTagName("TH")[0]; 
    _th.colspan = 2;

Upvotes: 1

Views: 433

Answers (2)

Kenney
Kenney

Reputation: 9093

It's colSpan, not colspan ;-)

See colSpan documentation.

(updated fiddle).

Upvotes: 4

Yehia Awad
Yehia Awad

Reputation: 2978

Javascript DOM elements has a special method for setting attributes, which is setAttribute('<attribute>','<value>')

_th.setAttribute("colspan", "2");

check this fiddle:

https://jsfiddle.net/Lgof8m6q/7/

Upvotes: 4

Related Questions