Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a html table highlight columns by changing the border on hover?

I'm exploring how to style a table in such a way that the border can be changed when the mouse hovers over a column.

enter image description here

When the mouse is over a column, I want to highlight that column by changing the border color:

enter image description here

To highlight, I am using the following JavaScript code with the jQuery library:

$('td').hover(function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').addClass('highlighted');
},
function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').removeClass('highlighted');
});

with the following CSS:

.highlighted {
    border-top: 2px solid black;
    border-right: 2px solid black;
    border-left: 2px solid black;
}

You can view how this works in this JSFiddle: http://jsfiddle.net/sCFjj/1/ This is only working when I hover on the left-most column. Otherwise it highlights the right-column (and top) but not the left vertical column. Is there a way of making the left-vertical column appear?

Ideally, I would like the effect to ignore the lowest row, but I am not sure how to exclude a row from the jQuery selection.

I've based my attempt closely on this previous question.

like image 410
djq Avatar asked Nov 21 '12 15:11

djq


People also ask

How do you change the border color of a table in HTML?

To change the border's color, use the attribute bordercolor="color" where color is the same format as all the other web colors we've been using. The table below has the bordercolor set to #ff00ff with the table tag <table bordercolor="#ff00ff">. To change the background color, use the attribute bgcolor="color".

What table attribute controls the border?

The HTML <table> border Attribute is used to specify the border of a table. It sets the border around the table cells.

Which attribute gives Colour to the border of a table?

With the border-color property, you can set the color of the border.

How do you customize a table border?

Go to Table Tools >Design > Table Styles > Borders, and then click the border option that you want to change.


1 Answers

Link: jsFiddle. Also add to previous collumn border-right and you will get that you want. I think that collumn's right border is over next collumn left border.

JavaScript:

$('td').hover(function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').addClass('highlighted');

    if(t>1){
        var t1 = t -1;
        //alert("T:" + t + "<br/> T1:" + t1);
        $('td:nth-child(' + t1 + ')').addClass('highlightedPrev');
    }
},
function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').removeClass('highlighted ');
        if(t>1){
         var t1 = t -1;
         $('td:nth-child(' + t1 + ')').removeClass('highlightedPrev');
    }
});​

CSS:

.highlighted {
    border: 2px solid black;
}
.highlightedPrev {
    border-right: 2px solid black;
}

Hope I solved your problem.

like image 153
Epsil0neR Avatar answered Oct 21 '22 14:10

Epsil0neR