471,305 Members | 1,131 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

about using <label>

8,656 Expert Mod 8TB

I just have one thing about the label element, where I’m not certain. <label> is commonly used to attach visual/text information to a form control. so far, so good.
Expand|Select|Wrap|Line Numbers
  1. <!-- just to make it visible -->
  2. <input type="checkbox" value="yes" id="mysql">
  3. <label for="mysql">save MySQL</label>
Now supposing I want to toggle the visibility of a <div> containing e.g. a description of something via JavaScript. although it is not exactly specified this way, button (and their labels) should be only written inside a <form> (that’s why they are form controls). on the other hand side, labels with their IDREF attribute for would make excellent "show content" elements … (even better than anchors, in the semantic sense)
Expand|Select|Wrap|Line Numbers
  1. <label for="desc1" class="trigger">show content</label>
  2. <div id="desc1" class="hide">bla bla blubb …</div>
  3. /* CSS */
  4. .hide {
  5.     display: none;
  6. }
  7. // JavaScript
  8. function toggle()
  9. {
  10.     // for the general idea …
  11.     var vis = document.getElementById(this.for).style.display;
  12.     if ("none" == vis) {
  13.         vis = "block";
  14.         this.textContent = "hide content";
  15.     } else {
  16.         vis = "none";
  17.         this.textContent = "show content";
  18.     }
  19. }
  20. // after some prototyping …
  21. document.getElementsByClassName("trigger").addEventForEach("click", toggle, false);
what do you think about this (mis)use?
Jan 4 '10 #1
3 1870
7,435 Expert 4TB
Nothing wrong with doing stuff like that if it works.
Jan 5 '10 #2
8,656 Expert Mod 8TB
good ole HTML forgives nearly everything …
Jan 5 '10 #3
105 100+
I've used custom attributes in some pages, thanks to HTMLs forgiving nature and they've proven invaluable in many places.
If you use jQuery for example, the HTML elements can be fetched by custom attributes, making the code very clean and flexible :)
Jan 19 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Xerxes | last post: by
5 posts views Thread by Brian Foley | last post: by
5 posts views Thread by Johnb41 | last post: by
3 posts views Thread by Josef K. | last post: by
reply views Thread by CharlesA | last post: by
7 posts views Thread by Smokey Grindle | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.