Make links clickable in tables
The Situation:
You use short codes cfdb-table or cfdb-datatable to display a table on your page. One of the table column contains URLs or email addresses. You would like those to be clickable.
The Solution:
Add the following jQuery javascript to your page. To do so, in WordPress, edit your post or page, and switch the editor to “Text” view. Then past in the script AFTER the short code appears.
URLs: Assuming you have a column named “Website” use this code. Change “Website” below to the name of your column in line 3.
1 2 3 4 5 6 7 8 | <script type="text/javascript"> (function ($) { $('td[title="Website"] div').each( function () { $(this).html('<a href="http://' + $(this).html() + '">' + $(this).html() + '</a>'); }) })(jQuery); </script> |
The above assumes you have a URL value like “www.google.com” and it add “http://” in front of it to create a valid link. If your links already have “http://” in it, simply delete that from the code above in line 5.
Emails: Assuming you have a column named “E-mail” use this code. Change “E-mail” below to the name of your column in line 3.
1 2 3 4 5 6 7 8 | <script type="text/javascript"> (function ($) { $('td[title="E-mail"] div').each( function () { $(this).html('<a href="mailto:' + $(this).html() + '">' + $(this).html() + '</a>'); }) })(jQuery); </script> |
New in version 2.8
Shortcodes in version 2.8+ allow you to add additional text before and after the short code. Include the above script with the short code like this (in between markers {{AFTER}} and {{/AFTER):
[cfdb-table form="myform"] {{AFTER}} <script type="text/javascript"> (function ($) { $('td[title="Website"] div').each( function () { $(this).html('<a href="http://' + $(this).html() + '">' + $(this).html() + '</a>'); }) })(jQuery); </script> {{/AFTER}} [/cfdb-table]
Thanks so much, Michael. I’ve tried it, and it works! But I wonder how to customize the code if I need to put two clickable links in my table?
I think there are a couple ways to do this:
1. Select both Column1 and Column2):
2. Or repeat the code for code for each column:
@Michael Simpson
Solved nicely. Thanks for help.
Hi,
would it be possible to have the word ‘Website’ in the table instead of the actual URL and have that word linked to the URL and clickable? URLs can be long and the table wouldn’t look nice.
best regards
Karolina
Yes, looking at the code sample I provide on the page, you would change the line:
to:
i.e. replace the last ‘ + $(this).html() + ‘ with Website
(but don’t put in the rel=”nofollow”…somehow this web site is forcing that into the code but it is no supposed to be in there.
Hi,
thanks for this great plugin! I am using it to display links to pdf or Word documents on the front end. If clicking on these links, I would like the document to open in the browser; but when I click on the link, it only allows me to save the document to my comp.
Is there a way to achieve this?
Thanks for your guidance.
Chris
Your browser decides what to do with file.