views:

1268

answers:

3

I want to have a table border (which I can set using css, rather than the inline border= attribute) to be set to border: 1px solid black; when I mouseover the table.

How do I go about doing this in jQuery. I think it's identical to what's happening to the buttons at the top of this page (Questions, Tags, Users etc) except that is a div having it's background color changing whereas I want to change the border of a table. But the concept is the same.

A: 

Check out this article on the mouseenter and mouseleave events.

$("table#mytable").mouseenter(function(){
      $("table#mytable",this).css("border", "solid 1px black");
    }).mouseleave(function(){
      $("table#mytable",this).css("border", "o");
    });
Michael La Voie
silly question I know, but does this go between the <script></script> tags
Ankur
Voted down. Excessive use of selectors. Slow performance on a large set of dom-nodes.
roosteronacid
yes, and in the ready() event also. I'll edit my post above
Philippe Leybaert
+3  A: 

For hovering effects, jQuery provides the hover() pseudo-event, which behaves better than moueseenter/mouseleave. Also, it's a good idea to create a CSS class for each state (normal and hovered), and then change the class on hover:

$(document).ready(function() {
    $("#tableid").hover(
        function() { $(this).addClass("Hover"); },
        function() { $(this).removeClass("Hover"); }
    );
});

Your CSS could look like this:

table.Hover { border: 1px solid #000; }
Philippe Leybaert
I'd cache the "this", since you're only selecting one table. No need to re-package that table in a jQuery object twice. Also; I'd use the mouseover/mouseout events instead of the hover-function. It's a bit more verbose, and I've found that the hover-function is a bit quirky at times.
roosteronacid
Thanks it works.
Ankur
var that = $("#tableid"); that.mouseover(function () {}); that.mouseout(function () {}); ... yeah. You could chain mouseover and mouseout. But I like to be verbose :)
roosteronacid
@roosteronacid: the hover() event is supposed to solve a lot of the issues with mouseenter/mouseleave (according to the jQuery guys). Regarding caching: in this example it's just one table, but it's just an example. In real life you would select multiple tables, so caching "this" is not an option
Philippe Leybaert
Sorry roosteronacid, I am very new, and I am wondering what "var that" is doing
Ankur
var is a JavaScript (not jQuery specific) keyword for defining a variable.
roosteronacid
@activa: Perhaps they've fixed the issues I was experiencing (delayed event triggering and multiple chained events).. It's been a while since I've used hover(). As far as caching goes; you're right. I guess I see it as a sort of mission to comment on things like that. I see many people not using jQuery "responsibly". From what I can understand; you're not one of 'em :)
roosteronacid
A related question: I want to repeat this effect on several tables but not all tables. Can I set the class of each of these tables to something like "hoverBorder" i.e. <table class="hoverBorder"> ... </table> and then replace $("#tableid").hover( with $("#hoverOver".hover( ? do you know what the syntax ought to be.
Ankur
Same code as activa wrote. Just target classes instead.. Like so: $(".classOne, .classTwo, etc.").hover( ...
roosteronacid
Or just target one class: $(".classnamegoeshere").hover( ...
roosteronacid
thanks, it's all working well.
Ankur
+1  A: 

It may be better to swap classes on the table instead of editing the CSS properties directly - that would be a more scalable/extendable solution:

table {
   border:0;
}

table.border {
   border:1px solid #000;
}

Of course your table will 'jump' in position 1px when the border is added so maybe it's better to use margin or a white border when you are not hovering:

table {
   border:1px solid #fff;
}

table.border {
   border:1px solid #000;
}

Or best yet, if you don't need to be compliant with IE6 you can do it all with pure CSS:

table {
   border:1px solid #fff;
}

table:hover {
   border:1px solid #000;
}

This would be the best/simplest/scalable approach!

Matthew James Taylor