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

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

  1. Hello I wanted to share a great international pharmacy where you can buy prescription drugs online. If you need cheap meds this site is the best choice. Secure shipping plus huge selection. Link here: https://onlinepharm.jp.net/#. Peace.

  2. Новости Днепра https://u-misti.dp.ua сегодня — актуальные события города происшествия экономика политика и общественная жизнь. Оперативные обновления важные решения властей и главные темы дня для жителей и гостей города.

  3. Городской портал https://u-misti.cherkasy.ua Черкасс — свежие новости события происшествия экономика и общественная жизнь. Актуальные обзоры городская хроника и полезная информация для жителей и гостей города.

  4. Hi I wanted to share a useful source for meds to order prescription drugs hassle-free. If you need no prescription drugs OnlinePharm is the best choice. They ship globally plus no script needed. Visit here: https://onlinepharm.jp.net/#. Thx.

  5. Greetings I just discovered a trusted website for cheap meds. If you are tired of high prices and need affordable prescriptions Pharm Mex is the best option. Great prices and it is safe. Check it out: https://pharm.mex.com/#. Kind regards.

  6. Hey everyone To be honest I found a useful online drugstore to order medications cheaply. If you are looking for antibiotics this store is highly recommended. Great prices and huge selection. Link here: online pharmacy usa. Regards.

  7. Поставляем грунт https://organicgrunt.ru торф и чернозем с доставкой по Москве и Московской области. Подходит для посадок благоустройства и озеленения. Качественные смеси оперативная логистика и удобные условия для частных и коммерческих клиентов.

  8. Hello I recently found a great source from India to buy generics. If you want to buy generic pills without prescription this store is very reliable. It has wholesale rates worldwide. More info here: this site. Best regards.

  9. Hey there I recently ran into an awesome online source to save on Rx. For those seeking and need meds from Mexico Pharm Mex is the best option. Fast shipping plus secure. Take a look: Pharm Mex. Hope this was useful.

  10. Hello To be honest I found a great source for meds for purchasing prescription drugs online. For those who need antibiotics this site is the best choice. They ship globally and it is very affordable. Link here: https://onlinepharm.jp.net/#. Be well.

  11. Hey there To be honest I found a useful source for meds where you can buy prescription drugs cheaply. For those who need cheap meds this site is worth a look. Great prices plus it is very affordable. Link here: buy meds online. Cheers.

  12. Op zoek naar een casino? WinItt Casino biedt online gokkasten en live games. Het biedt snel inloggen eenvoudige navigatie moderne speloplossingen en stabiele prestaties op zowel computers als mobiele apparaten.

  13. joycasino промокод зеркало джойказино

  14. Hey there To be honest I found a useful international pharmacy where you can buy prescription drugs securely. For those who need cheap meds this store is the best choice. Fast delivery plus no script needed. Link here: Online Pharm Store. I hope you find what you need.

  15. Greetings To be honest I found a useful website to order medications securely. For those who need antibiotics OnlinePharm is the best choice. Fast delivery and huge selection. Check it out: https://onlinepharm.jp.net/#. Good luck

  16. To be honest I just came across an awesome online source for cheap meds. If you want to save money and need meds from Mexico Pharm Mex is the best option. No prescription needed plus secure. Visit here: buy meds from mexico. Hope this helps

  17. Hey there I recently ran into a reliable website for cheap meds. If you are tired of high prices and need meds from Mexico Pharm Mex is highly recommended. Fast shipping and it is safe. Take a look: cheap antibiotics mexico. Best regards.

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>