Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underline a Hyperlink On Hover using jQuery

Tags:

jquery

I used the method

$("#dvTheatres a").hover(function (){
        $(this).css("text-decoration", "underline");
    },function(){
        $(this).css("text-decoration", "none");
    }
);

Is there a more elegant method?(single line)

like image 459
naveen Avatar asked Nov 01 '08 04:11

naveen


People also ask

What does the hover function in jquery do?

The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.

What text decoration value will remove the underline from a link?

By setting the text-decoration to none to remove the underline from anchor tag. Syntax: text-decoration: none; Example 1: This example sets the text-decoration property to none.


2 Answers

Why not just use CSS?

#dvTheatres a {
    text-decoration: none;
}

#dvTheatres a:hover {
    text-decoration: underline;
}
like image 70
Paige Ruten Avatar answered Oct 02 '22 08:10

Paige Ruten


You might be having issues with other CSS rules overriding the one you want. Even if it is declared last in the file, other declarations might have more importance and hence your will be ignored. eg:

#myDiv .myClass a {
    color: red;
}
#myDiv a {
    color: blue;
}

Because the first rule is more specific it takes precedence. Here's a page explaining CSS Specificity: http://www.htmldog.com/guides/cssadvanced/specificity/

The reason your jQuery solution works is because applying a style via the style="" parameter has very high specificity.

The best way to find which rules are being applied and which are being overruled by others is to use the Firebug extension for Firefox. Inspect the element in that and click on the CSS tab: it will show you every single CSS declaration which is being applied, and put a strike-through on ones which are being overruled.

If you want a really quick and easy way to solve your problem though, try this:

#dvTheatres a:hover {
    text-decoration: underline !important;
}

if you really want to stick to using jQuery, your method is fine and probably the most elegant way to do it (using jQuery).

like image 29
nickf Avatar answered Oct 02 '22 09:10

nickf