Opening pdf links in a new window using jQuery

This is probably very simple and stupid, but I'm trying to use jQuery to open all pdf files in new windows. I used the code below and got this error:

uncaught exception: syntax error, unrecognized expression: [href $ =. pdf]

I am sure any help would be appreciated.

Note. I came under jQuery for $ because I use Wordpress.

<script type="text/javascript">
jQuery(function() {
jQuery("a[href$=.pdf]").click(function() {
window.open(this.href);
}); 
}); 
</script>
+5
source share
1 answer

Try the following:

jQuery(function($) {
    $('a[href$=".pdf"]').attr('target', '_blank');
}); 
+17
source

All Articles