A jQuery snippet to hide unnecessary support
We had a list of links that wouldn’t sit still. The client would occasionally change the links, including some to PDF files. To accommodate users, we added a link to Adobe Reader. But when the list had no PDFs, the Reader link became superfluous.
Conventional wisdom would have left the Reader link on the page no matter what. Users are free to ignore what they don’t need. But in our campaign against clutter, we wanted to remove the link when the page contained no PDFs.
To keep from checking for PDFs manually, I wrote this quick jQuery function.
Sample HTML
<p><a href="test.html">link 1</a></p>
<p><a href="test.html">link 2</a></p>
<p><a href="test.pdf">link 3</a></p>
<p><a href="test.html">link 4</a></p>
<div id="pdf_link"><a href="http://get.adobe.com/reader/">Get Adobe Reader</a></div>
The jQuery
function bt_reveal_support ( condition, support_id ){
var x = $(condition).length;
if ( x > 0 ) {
$(support_id).show();
}
else {
$(support_id).hide();
}
}
bt_reveal_support('a[href$=pdf]', '#pdf_link');
In English
Count every anchor element (that is, every link) that ends with “pdf”. If there’s more than one, then show the appropriate link to Adobe Reader. If not, hide the Adobe Reader link.