jQuery to style Hyperlinks based on URL extension

Yesterday I was working on a requirement where I need to apply different styles to hyperlinks based on their URL extensions. For example, if there are hyperlinks which redirects to ".pdf" extension then change their font color to "Red" so that they look different from other hyperlinks.

This was easy to implement using jQuery. What is required is to select hyperlinks based on their extension and then apply CSS. Below piece of jQuery code based on URL extension, assign font color.

$(document).ready(function() {
    $("a[href$=pdf]").css("color", "Red");
    $("a[href$=png]").css("color", "green");
    $("a[href$=jpg]").css("color", "blue");
    $("a[href$=html]").css("color", "maroon");

See result below

In this jQuery code, use "$" sign at the end of href property which selects all the hyperlinks with href property value ending with "pdf", "jpg", "png" or "html" string. And once found then do the rest.

See Complete Code

Feel free to contact me for any help related to jQuery, I will gladly help you.

Responsive Menu
Add more content here...