Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 07:40 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Returning the name of the column Header When Clicked

Hello,

I really need your help and am fairly new to javascript.

Given the html table below, how can I create a javascript function that will, at the click of a mouse, alert me as to what the name of the column header is?

Ie. if I click on the COLORS header, a javascript box will popup and alert("COLORS")?

I am not sure where to begin. I am using IE. 7 so it would have to be compatible

Code:
<html> 
 
<head> 

</head> 
 
<body> 
 
<table border="1" cellspacing="1" width="500"> 
    <tr> 
        <td>FRUITS</td> 
        <td>COLORS</td> 
        <td>VEGGIES</td> 
        <td>NUMBERS</td> 
    </tr> 
    <tr> 
        <td>apples</td> 
        <td>red</td> 
        <td>carrots</td> 
        <td>123</td> 
    </tr> 
    <tr> 
        <td>oranges</td> 
        <td>blue</td> 
        <td>celery</td> 
        <td>456</td> 
    </tr> 
    <tr> 
        <td>pears</td> 
        <td>green</td> 
        <td>brocoli</td> 
        <td>789</td> 
    </tr> 
    <tr> 
        <td>mangos</td> 
        <td>yellow</td> 
        <td>lettuce</td> 
        <td>098</td> 
    </tr> 
</table> 
 
</body> 
 
</html>
jason_kelly is offline   Reply With Quote
Old 10-22-2012, 10:38 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,198
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
<html> 
<head> 
</head> 
<body> 
<table id="MyTable" border="1" cellspacing="1" width="500"> 
    <tr> 
        <td>FRUITS</td> 
        <td>COLORS</td> 
        <td>VEGGIES</td> 
        <td>NUMBERS</td> 
    </tr> 
    <tr> 
        <td>apples</td> 
        <td>red</td> 
        <td>carrots</td> 
        <td>123</td> 
    </tr> 
    <tr> 
        <td>oranges</td> 
        <td>blue</td> 
        <td>celery</td> 
        <td>456</td> 
    </tr> 
    <tr> 
        <td>pears</td> 
        <td>green</td> 
        <td>brocoli</td> 
        <td>789</td> 
    </tr> 
    <tr> 
        <td>mangos</td> 
        <td>yellow</td> 
        <td>lettuce</td> 
        <td>098</td> 
    </tr> 
</table> 
 
<script type="text/javascript">
(
  function( )
  {
      var tbl = document.getElementById("MyTable");
      var row1 = tbl.rows[0];
      var tds = row1.cells;
      for ( var t = 0; t < tds.length; ++t )
      {
          tds[t].style.cursor = "hand"; // OPTIONAL, but a nice idea
          tds[t].onclick = function( ) { alert(this.innerHTML); }
      }
  }
)( );
</script>

</body> 
 </html>
Works in all browsers at least back to MSIE 7.
__________________
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
jason_kelly (10-25-2012)
Old 10-25-2012, 01:48 AM   PM User | #3
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Thanks very much. Worked Like a charm Thanks again for helping me out.
jason_kelly is offline   Reply With Quote
Reply

Bookmarks

Tags
dom, html, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.