Skip to content

Instantly share code, notes, and snippets.

@jirivrany
Created December 10, 2013 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirivrany/7888993 to your computer and use it in GitHub Desktop.
Save jirivrany/7888993 to your computer and use it in GitHub Desktop.
JQuery enhance links with class by file type
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
a {
color: orange;
}
.pdf {
color: red;
}
.doc {
color: green;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready( function() {
var endsWith = function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
$("a").each( function(){
if ( endsWith(this.href, ".pdf") ) {
$(this).addClass( "pdf" );
}
else if ( endsWith(this.href, ".doc") ) {
$(this).addClass( "doc" );
}
});
}
);
</script>
<body>
<p>
<a href="ahoj.html">ahoj</a>
</p>
<p>
<a href="soubor.pdf">pdf file</a>
</p>
<p>
<a href="word.doc">se s tim smiř</a>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment