Prepares a string for use as a valid HTML ID and guarantees uniqueness.

Prepares a string for use as a valid HTML ID and guarantees uniqueness.

This function ensures that each passed HTML ID value only exists once on the page. By tracking the already returned ids, this function enables forms, blocks, and other content to be output multiple times on the same page, without breaking (X)HTML validation.

For already existing IDs, a counter is appended to the ID string. Therefore, JavaScript and CSS code should not rely on any value that was generated by this function and instead should rely on manually added CSS classes or similarly reliable constructs.

Two consecutive hyphens separate the counter from the original ID. To manage uniqueness across multiple Ajax requests on the same page, Ajax requests POST an array of all IDs currently present on the page, which are used to prime this function's cache upon first invocation.

To allow reverse-parsing of IDs submitted via Ajax, any multiple consecutive hyphens in the originally passed $id are replaced with a single hyphen.


matslats’s picture

As Ryan Szrama says:

if you need to do full form replacement yourself, be sure not to target the form using its id. Each time it is rebuilt, the HTML id is incremented by drupal_html_id(), so the replacement will only work the first time. Instead, either target a container within the form or some other selector that won't change each refresh.

chrisjlee’s picture

jrb’s picture

Why does this function replace underscores with dashes? The w3.org comment says that underscores are OK, and the first call to preg_replace() explicitly allows them. Is there a reason?

seutje’s picture

To be fair, it's quoting a spec that was last updated in 1999 and has been superseded since. Meanwhile, half the codebase isn't conform that 1999 spec anymore, because html5 and stuff! Consistency++

jrb’s picture

It appears to have originally come from this issue: https://www.drupal.org/node/464862

"We currently don't use underscores in class names, so this new function should replace them with dashes by default, but allow callers to relax that restriction if they have a use case (like integrating with 3rd party code.)"

We seem to have lost the ability to "relax" this.

davidwbarratt’s picture

This function does not seem to prevent an ID from starting with a numeric digit (or any other non latter character): http://stackoverflow.com/a/7987693

Is there a work around to this problem?


seutje’s picture

The solution is simple: don't care, browsers don't care either.

hayk.hayrapetyan’s picture

Hi all,
Maybe my question has very simple answer, but I can't find the solution 5 days.
I write the module, with form. The form has drop-down menu. the drop-down menu items I select from mysql db courses table course array. In mysql I have this tables:
id, course, description, amount.
My problem is very simple, I want to select from db course description, after selecting the course from drop-down and present before the drop-down menu.

Sorry, Thx

jonhattan’s picture