4

Is it possible to create a link using Drupal's l() function without an href? For example, I'd like to create a link like the following:

<a name="my-anchor">A link here</a>

However, this seems to not be possible. The closest I can come is to use the following:

l(t('A link here'), '', array('external' => TRUE, 'attributes' => array('name' => 'my-anchor')));

This outputs:

<a href name="my-anchor">A link here</a>

... and that's not valid HTML. It also causes slightly strange behavior in some browsers depending on the CSS applied...

3 Answers 3

6

Sadly this cannot be accomplish with the l function because the href is hard-coded in the theme_link theme function.

You can accomplish this by using the html_tag theme function.

It'll look something like this

return theme('html_tag', array('element' => array('#tag' => 'a', '#value' => 'A link here', '#attributes' => array('name' => 'my-anchor'))));

This will return

<a name="my-anchor">A link here</a>

Is a bit more work, but it does give you a properly formatted a tag with out the href.

3

It cannot be done with l() only, since the href attribute is hard-coded in theme_link().

You could, however, override this them function, and add an extra key to the options array (something like 'anchor' => TRUE').

2
  • Basically, yes. I up toed, but chose Eric's answer instead, as it's an easier solution for a one-off use. Jun 23, 2011 at 5:47
  • s/up toed/upvoted Jun 25, 2014 at 21:29
0

I don't believe you can create an <a> tag with Drupal without the href attribute. Doing so would be invalid HTML as href is a required attribute for <a> tags so it is not something I would suggest doing anyways.

Why don't you create a tag with href="#"or something similar?

1

Your Answer

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

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