Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery prepend to all hyperlink elements that link to PDF files

I would like to prepend "Download a PDF of " to any hyperlinks, linking to PDF files. Currently I'm able to prepend that exact text, but it prepends it to the hyperlink text. I would like it to reside outside of the hyperlink element, like so: Download a PDF of [hyperlink with text]

This is the code I'm using now:

jQuery('a[href$=.pdf]').prepend('Download a PDF of ');
like image 879
Hunter Satterwhite Avatar asked Dec 14 '22 00:12

Hunter Satterwhite


2 Answers

Have you tried before?

jQuery('a[href$=.pdf]').before('Download a PDF of ');
like image 101
AlteredConcept Avatar answered Dec 15 '22 13:12

AlteredConcept


The other selectors that have been provided in answers are wrong, as of today anyway. jQuery will complain:

Uncaught Error: Syntax error, unrecognized expression: a[href$=.pdf]

The right way to select the anchor is:

jQuery('a[href$=".pdf"]');

Note the quotes around .pdf

like image 25
JRT Avatar answered Dec 15 '22 13:12

JRT