0

Possible Duplicate:
MSIE and addEventListener Problem in Javascript?
Returning the Name of a column header

For the life of me, and to the extent of my knowledge with javascript, I can't seem to get this go, its failing at line 13. Im basically trying to attempt to add an onclick event to a TH to alert me of its value. Im using IE7.

<script type="text/javascript">



function init() {

var titles = document.getElementsByTagName("th"); 

for ( var i = 0, len < titles.length; i < len; i++ ) {    
    titles[i].addEventListener("click", function() { 
        alert( this.innerHTML ); 
    }, false); 
} 

}//end of function

</script>

</head>

<body onload="init()">

<table border="1" cellspacing="1" width="500">
    <tr>
        <th>FRUITS</th>
        <th>COLORS</th>
        <th>VEGGIES</th>
        <th>NUMBERS</th>
    </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>
Community
  • 1
  • 1
Jason Kelly
  • 2,539
  • 10
  • 43
  • 80

1 Answers1

0

You have an error in your for statement, you were incorrectly assigning to len variable, so replace your following line:

for ( var i = 0, len < titles.length; i < len; i++ ) {

for this one:

for ( var i = 0, len = titles.length; i < len; i++ ) {
Nelson
  • 49,283
  • 8
  • 68
  • 81