Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    193
    Thanks
    147
    Thanked 0 Times in 0 Posts

    Returning the name of the column Header When Clicked

    Hello,

    I need the help of an expert out there.

    How can the existing code be modified below, so as to return the name of the column header when the column header box is clicked by the user.

    Ie. if the column header "File Number" box is clicked by the user, a javascript alert box will alert them as to what the name of the column header (File Number) ect. for each of the column headers.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    #mstrTable {
         border: 1px solid black
    }
    #mstrTable td, th {
         border: 1px solid black
    }
    
    #mstrTable tr.normal td {
        color: black;
        background-color: white;
    }
    #mstrTable tr.highlighted td {
        color: white;
        background-color: gray;
    }
    </style>
    </head>
    <body>
      <table id="mstrTable">
         <thead>
          <tr> 
            <th>File Number</th>
            <th>Date1</th>
            <th>Date2</th>
            <th>Status</th>
            <th>Num.</th>
          </tr>
        </thead>
        <tbody>
          <tr> 
            <td>KABC</td>
            <td>09/12/2002</td>
            <td>09/12/2002</td>
            <td>Submitted</td>
            <td>0</td>
    
          </tr>
          <tr> 
            <td>KCBS</td>
            <td>09/11/2002</td>
            <td>09/11/2002</td>
            <td>Approved</td>
            <td>1&nbsp;</td>
          </tr>
    
          <tr> 
            <td>WFLA</td>
            <td>09/11/2002</td>
            <td>09/11/2002</td>
            <td>Submitted</td>
            <td>2</td>
          </tr>
          <tr> 
            <td>WTSP</td>
            <td>09/15/2002</td>
            <td>09/15/2002</td>
            <td>In-Progress</td>
            <td>3</td>
          </tr>
        </tbody>
      </table>
    
    <script type="text/javascript">
    (
      function( )
      {
          var trows = document.getElementById("mstrTable").rows;
    
          for ( var t = 1; t < trows.length; ++t )
          {
              trow = trows[t];
              trow.className = "normal";
              trow.onclick = highlightRow;
          }
    
          function highlightRow(e)
          {
          alert('Row is ' + (this.rowIndex-1))
              for ( var t = 1; t < trows.length; ++t )
              {
                  trow = trows[t];
                  trow.className = ( trow == this && trow.className != "highlighted") ? "highlighted" : "normal";
              }
          }
      }
    )();
    </script>
    </body>
    </html>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    in keeping with what looks alot to me like Old Pedant's code, put this after the var trows line:

    Code:
    var hdrs=trows[0].cells;
    	  
    	  for ( var h = 0; h < hdrs.length; ++h )
          {
    	  hdrs[h].onclick = function(){alert(this.innerHTML)}
          }

  • Users who have thanked xelawho for this post:

    jason_kelly (01-15-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    But now the bigger question: Have you not been reading this forum for a while? Have you not read many many times that alert( ) is obsolete and a really really bad thing to use?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •