I use Slack, and one think we love to do is share new articles and discuss them. But the good old copy and past the link was annoying me, so I created a Bookmarklet that will share your current page to a selected channel.
This is my personal blog and mainly contains technical solutions, open source code, and other projects I have worked on. Much of the technical solutions are very niche so your milage may vary.
Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts
Friday, December 4, 2015
Monday, October 1, 2012
Automatic Unobtrusive Binding of Labels to Form Elements
One commonly forgotten feaure in HTML is that when creating web forms the labels related to input elements should be related to them. In the simplest terms it is done like so:
Thus I have the following bit of javascript to automatically do the same thing. It looks for any unbound labels and binds them to the next input, text area, or select item down the DOM tree from them. That is the next matching element within the same parent that the label is in. This method will also work if the input fields don't have ID's associated with them as an added bonus.
To use just include this in a global script running on your site and it will do the work with no noticeable overhead. This does require jQuery to work.
<label for="myItem">My Label</label><input id="myItem">
The benifit to doing this is that browser will automatically focus the input element when the label is selected. A little thing I know but it's not use in most sites doe to inconvenience.Thus I have the following bit of javascript to automatically do the same thing. It looks for any unbound labels and binds them to the next input, text area, or select item down the DOM tree from them. That is the next matching element within the same parent that the label is in. This method will also work if the input fields don't have ID's associated with them as an added bonus.
To use just include this in a global script running on your site and it will do the work with no noticeable overhead. This does require jQuery to work.
$(document).ready(function () {
$("form label").each(function() {
if (typeof $(this).attr("for") == "undefined" || $("#" + $(this).attr("for")).length == 0) {
nextControl = $(this).next("input,textarea,select");
if (nextControl.length > 0) {
if (typeof nextControl.attr("id") != "undefined") {
$(this).attr("for", nextControl.attr("id"));
} else {
$(this).removeAttr("for");
$(this).click(function() {
$(this).next("input,textarea,select").focus()
});
}
}
}
});
});
Subscribe to:
Posts (Atom)
Project Licenses
These works by Jeremy Pyne are licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License