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

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

  1. top 10 pharmacies in india: top 10 online pharmacy in india – best rx pharmacy online

  2. This is the perfect blog for everyone who really wants to understand this topic. You know a whole lot its almost tough to argue with you not that I personally will need to…HaHa. You definitely put a fresh spin on a subject which has been written about for ages. Excellent stuff just great adult xxx video porn site xxx sex video

  3. Easy Mex Meds: mexico prescription online – purple pharmacy mexico

  4. Online medicine home delivery: Easy India Meds – buy online medicine

  5. Thanks a lot for sharing this with all of us you actually recognise what you are speaking about Bookmarked. Please additionally consult with my website =). We will have a hyperlink trade agreement between us Watch sexual porno video xxx sex adults site

  6. canadian pharmacy service canadian drugstore online Easy Canada Meds

  7. Hi I am so delighted I found your weblog I really found you by accident while I was looking on Aol for something else Anyhow I am here now and would just like to say thanks for a tremendous post and a all round interesting blog I also love the theme/design I dont have time to read it all at the minute but I have bookmarked it and also added in your RSS feeds so when I have time I will be back to read a great deal more Please do keep up the excellent work. 在线购买无处方安定片 xxx Pornhub

  8. остекление домов в серпухове Если ваш балкон в Серпухове нуждается в обновлении или вы мечтаете о большем пространстве благодаря остеклению лоджии в Серпухове наши специалисты помогут вам воплотить любые идеи.

  9. Easy Canada Meds: canadian online pharmacy no prescription – canadapharmacyonline legit

  10. Easy Canada Meds: Easy Canada Meds – pharmacy in canada

  11. reputable indian pharmacies Easy India Meds best online pharmacy no prescription

  12. canada drugs online review: Easy Canada Meds – pharmacy com canada

  13. Easy Canada Meds: canadapharmacyonline legit – ed drugs online from canada

  14. phentermine in mexico pharmacy: Easy Mex Meds – best mexican online pharmacy

  15. indian pharmacy online: buy medicines online in india – trustworthy online pharmacy

  16. Easy Canada Meds canada drugs online Easy Canada Meds

  17. buy prescription drugs from canada cheap: canadian pharmacy scam – reputable canadian online pharmacies

  18. online mexican pharmacy: pharmacy online – Easy Mex Meds

  19. mexico medication: Easy Mex Meds – Easy Mex Meds

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>