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.

Ankur
  • 50,282
  • 110
  • 242
  • 312

4 Answers4

11

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
  • 168,566
  • 31
  • 210
  • 223
  • 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. – cllpse Jun 04 '09 at 06:15
  • var that = $("#tableid"); that.mouseover(function () {}); that.mouseout(function () {}); ... yeah. You could chain mouseover and mouseout. But I like to be verbose :) – cllpse Jun 04 '09 at 06:16
  • 1
    @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 Jun 04 '09 at 06:19
  • Sorry roosteronacid, I am very new, and I am wondering what "var that" is doing – Ankur Jun 04 '09 at 06:19
  • var is a JavaScript (not jQuery specific) keyword for defining a variable. – cllpse Jun 04 '09 at 06:23
  • 1
    @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 :) – cllpse Jun 04 '09 at 06:27
  • 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. ...
    and then replace $("#tableid").hover( with $("#hoverOver".hover( ? do you know what the syntax ought to be.
    – Ankur Jun 04 '09 at 06:27
  • Same code as activa wrote. Just target classes instead.. Like so: $(".classOne, .classTwo, etc.").hover( ... – cllpse Jun 04 '09 at 06:30
2

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
  • 4,806
  • 5
  • 29
  • 33
0

Alternatively, "outline" as opposed to "border" will not take up extra space in the element layout.

Tim M. Hoefer
  • 151
  • 1
  • 3
0

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
  • 27,772
  • 14
  • 72
  • 92