I’m having an issue wrapping a link around some output in a template file. I have the right template file; and I can put text like ‘start here’ and ‘end here’ and it prints out where I want the ‘a href’ tag to start and end.

However when I put in the starting ‘a href’ it auto closes. I can put in a ‘div’ and that doesn’t auto close. I’ve tried <div href="javascript:void(0)" onclick="window.location.assign("'.$link.'")” > and although that html output looks right it doesn’t work either.

I’ve also used at ‘function l’ but can’t pass the $content in.

Any ideas how I can either stop the auto close, get the javascript to work or a function I can use to output the link but will except the content being passed in.

Here's my code

print '<a href="'.$link.'" >'; 
     print $field->content;
     print '</a>'; 
  • Can you mention the exact requirement because you can not write "href" attribute in <div> its for <a>.
    Commented Sep 12, 2014 at 8:07
  • I'm trying to make a whole div clickable .
    – Deejay
    Commented Sep 12, 2014 at 8:15
  • 1
    If you wants whole <div> clickable. Instated of putting a "div" as wrapper you can write a css for your <a> tag. It will works as <div>. just put a class and set the css property as display:block.
    Commented Sep 12, 2014 at 8:22
  • the 'a tag' auto closes, I don't know why it does this. . Otherwise the code would work fine. so output is <a href="mylink.html"></a> my content </a>
    – Deejay
    Commented Sep 12, 2014 at 8:40
  • 2
    Does the content that you are wrapping have an anchor tag inside it? This could be a hidden anchor that you will only see when inspecting source. I'm asking because you cant nest anchors. Commented Sep 12, 2014 at 10:39

2 Answers 2


The problem here was that there where links in the content being outputted. Once I changed that the links stopped auto closing.


Use filter_xss on $field->content. You can filter out the anchor tags this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.