jQuery plugin to associate label “for” attribute with form controls that have dynamic id’s

For whatever reason, you can’t associate your labels with your form controls implicitly by wrapping both the label text and the control in a <label> element:

<label>
  Email address: <input type="email" name="email" value="" placeholder="you@example.com"/>
</label>

You’ve had to explicitly state the form control you want the label to be associated with:

<label for="email">Email address:</label>
<input id="email" type="email" name="email" value="" placeholder="you@example.com"/>

…but your framework has other ideas, it decides to commandeer your precious “id” attribute and change it from a simple and beautiful “email” into a junky “dnn_ctr1163_RegistrationForm_email”. *cough* DotNetNuke *cough*, but also *cough* Wicket *cough* and probably a whole load of others.

The problem is now that your label is no longer associated with your form control, and there isn’t a simple fix, like, for example, change the for attribute to read “dnn_ctr1163_RegistrationForm_email”, because the numbers in the id change each time you refresh the page. The ID is dynamic, and you are receiving accessibility black stars.

So, luckily I noticed that often the id will change, but the framework will leave your originally intended id somewhere in there. I’ve written a jQuery plugin to look at label for attributes and try to find the form control you intended to associate it with before your server side framework so rudely changed it.

How to use

  1. Download the plugin and add it to your page
  2. Select the labels you want to re-associate and call the function eg. $(‘label’).fuzzyFor();

TODO

The plugin finds matches by looking for input, select and textarea elements in the document and checking to see if the for attribute in your label element appears in the id attribute of the form control. At the moment if more than one match is found, it just uses the first (which is probably good enough for most). However, it should probably do something a bit more clever and use the form control that is closest to the label (I’m reasoning that you normally put your label’s close to your controls).

4,639 thoughts on “jQuery plugin to associate label “for” attribute with form controls that have dynamic id’s

  1. pet pharmacy: VetFree Meds – pet prescriptions online

  2. I blog quite often and I truly thank you for your content. This great article has really peaked my interest. I will book mark your website and keep checking for new details about once per week. I subscribed to your Feed as well. Crypto Market Data

  3. рейтинг хостинговых компаний самый надёжный хостинг лучший хостинг

  4. online vet pharmacy: VetFree Meds – online pet pharmacy

  5. необычные экскурсии спб Экскурсии СПб Санкт-Петербург охватывают все: от Эрмитажа до Петергофа с удобным бронированием и гарантией комфорта для семей и одиночек.

  6. purple pharmacy online ordering: My Mexican Pharmacy – My Mexican Pharmacy

  7. CertiCanPharmacy: canadian online pharmacy – CertiCanPharmacy

  8. purple pharmacy: My Mexican Pharmacy – My Mexican Pharmacy

  9. лучший хостинг в россии рейтинг хостингов ddos защита какой хостинг лучше

  10. CertiCanPharmacy: canada pharmacy online – canadian drug pharmacy

  11. pet meds for dogs: pet drugs online – п»їdog medication online

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>