View Full Version : Add REL to LI onClick?

03-11-2009, 07:41 PM
I am using a JavaScript to add a 'blank' target to any links that have 'external' specified in the REL. This is working great, but on some of the links, I need to give the user the ability to click the surrounding <li> item as well.

So my regular link looks like this:

<li><a rel="external" href="http://www.google.com">Google</a></li>

I need to figure out how to add the REL to the onclick in the LI

<li onclick="location.href='http://www.google.com/';"><a rel="external" href="http://www.google.com">Google</a></li>

The JavaScript I am using is here:

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";

THANKS FOR ANY ADVICE! I can't seem to find any instructions about how to insert this REL option....

03-11-2009, 07:50 PM
why not just display your <a> block so that it just fills in the entire <li>?

03-11-2009, 08:13 PM
I've got a li:hover working on the entire LI....


03-11-2009, 08:44 PM
Then just use:

<li onclick="window.open('http://www.google.com/');"><a rel="external" href="http://www.google.com">Google</a></li>

03-11-2009, 11:49 PM
That works, but it causes two windows to pop open when the user clicks on the link! I think I am actually just going to skip the onClick for these particular links and force the user to actually hit the hyperlinked text....usability-wise, that will be better.

Thanks for the help!

03-12-2009, 02:12 AM
you could also use this which would solve your problem

<li onclick="this.getElemetsByTagName('a').click();"><a rel="external" href="http://www.google.com">Google</a></li>

03-12-2009, 10:20 PM
Hmmm - gave that a shot, but no dice....