HTML | DOM TableData colSpan Property

The Dom TableData colSpan Property in HTML DOM is used to set or return the value of a colspan attribute. The colspan attribute is used to specify the number of columns a table should span.

Syntax:

  • It returns the colSpan property.
    tabledataObject.colSpan
  • It is used to set the colSpan property.
    tabledataObject.colSpan = number

Property Values: It contains the numeric value which specifies the number of columns a cell should span.



Return Value: It returns a string value which represents the number of columns that a cell should span.

Example-1: This Example returns a colspan Property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        table,
        th,
        td {
            border: 1px solid green;
        }
    </style>
</head>
  
<body>
  
    <body>
  
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
  
        <h2>
          DOM TableData Colspan Property
      </h2>
  
        <table>
            <tr>
                <td id="myTd"
                    colspan="3">
                  Geeks
              </td>
                <td>For</td>
                <td>Geeks</td>
            </tr>
        </table>
        <br>
        <button onclick="myFunction()">
            Click Here!
        </button>
  
        <p id="demo" 
           style="font-size:24px;
                  colorgreen;">
      </p>
  
        <script>
            function myFunction() {
                // Accessing Table data 
                var x =
                    document.getElementById(
                      "myTd").colSpan;
                document.getElementById("demo").innerHTML = x;
            }
        </script>
    </body>
  
</html>

chevron_right


Output:
Before Clicking On Button:

After Clicking On Button:

Example-2: This Example sets the colspan Property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        table,
        th,
        td {
            border: 1px solid green;
        }
    </style>
</head>
  
<body>
  
    <body>
  
        <h1 style="color:green;"
            GeeksForGeeks 
        </h1>
  
        <h2>
          DOM TableData Colspan Property
      </h2>
  
        <table>
            <tr>
                <td id="myTd" 
                    colSpan="3">
                  Geeks
              </td>
                <td>For</td>
                <td>Geeks</td>
            </tr>
        </table>
        <br>
        <button onclick="myFunction()">
            Click Here!
        </button>
  
        <p id="demo" 
           style="font-size:24px;
                  colorgreen;">
      </p>
  
        <script>
            function myFunction() {
                // Accessing Table data 
                var x =
                    document.getElementById("myTd").colSpan = "5";
                
                document.getElementById("demo").innerHTML = 
                  "The value of the Colspan "+
                  "attribute was changed to " 
                + x;
            }
        </script>
    </body>
  
</html>

chevron_right


Output:
Before Clicking On Button:

After Clicking On Button:

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.