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...

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').

  • Basically, yes. I up toed, but chose Eric's answer instead, as it's an easier solution for a one-off use. – geerlingguy Jun 23 '11 at 5:47
  • s/up toed/upvoted – geerlingguy Jun 25 '14 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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