tags:

views:

28

answers:

1

I dynamically added rows to a table but it doesn't seem to take hover effects given in jquery...

Here is my code,

 <table  id="chkbox" cellpadding="0" cellspacing="2" width="100%" class="table_Style_Border">
        <tr>
            <td style="width:150px" class="grid_header" align="center">RackName</td>    
            <td style="width:150px" class="grid_header" align="center">LibraryName</td>  
            <td style="width:200px" class="grid_header" align="center">LibrarianName</td>
            <td style="width:200px" class="grid_header" align="center">Location</td>
            <td style="width:1%" class="grid_header"></td>


        </tr>

         <? if(isset($comment))
               { echo '<tr><td class=table_label colspan=5>'.$comment.'</td></tr>'; } ?>
        <?php foreach($rackData as $key =>  $row) { ?>
        <?php printf('<tr  class="%s">', ($key % 2) ? 'rowcolor' : 'alternaterowcolor'); ?>
            <td align="left" class="table_label">
                    <?=$row['rack_name']?>
            </td>
            <td align="left" class="table_label">
                    <?=$row['library_name']?>
            </td>
            <td align="center" class="table_label">
                    <?=$row['librarian']?>
            </td>
            <td align="center" class="table_label">
                    <?=$row['location']?>
            </td>
            <td align="center">
                <input type="checkbox" name="group" id="group" value="<?=$row['rack_id']?>" onclick="display(this);"  > 
            </td>

        </tr>

<?  } ?>


        </table><div align="right" class="pagination"> <?php /*?><?php echo $links;?><?php */?></div>
<script type="text/javascript">
$("#chkbox").hover(function() {
  $(this).addClass("resultshover");
}, function() {
  $(this).removeClass("resultshover");
});
</script>
+1  A: 

Your code looks correct for the most part, but I believe the problem is you must call your Javascript in the $(document).ready() function, like this:

$(document).ready(function() {
    $('#chkbox tr').hover(function() {
        $(this).addClass('resultshover');
    }, function() {
        $(this).removeClass('resultshover');
    });
});
wsanville
@wsanville it comes for entire table not for rows
chandru_cp
I wasn't sure by your question if you wanted the entire table or just the rows. My answer has been updated for the rows. You just need to change the selector to '#chkbox tr'
wsanville
@wsanville i should have asked as table row instead of table.... Its working
chandru_cp
@chandru_cp: for slightly better performance, I'd recommend using a css style for the hover if your element's id and/or class is already known. That would mean less javascript processing for the client, and one additional line of stylesheet instead of 7 lines of javascript. e.g. `#chkbox tr:hover { /* hover styles */ }`. However, `CSS 2.1 does not define if the parent of an element that is ':active' or ':hover' is also in that state` see: http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
Jim Schubert