Code Snippet
Trigger Click on Input when Label is Clicked
Labels should have "for" attributes that match the ID of the input they are labeling. This means we can snag that attribute and use it in a selector to trigger a click on the input itself. Assuming of course you have some reason to watch for clicks on inputs.
var labelID;
$('label').click(function() {
labelID = $(this).attr('for');
$('#'+labelid).trigger('click');
});
Not sure if that’s what you mean, but Safari does this built-in.
All browsers except IE will do this when you code it this way:
<label><input id='my_input'/></label>
IE requries this:
<label for='my_input'><input id='my_input'/></label>
Yeah I really don’t see the point in this jQuery code. Using the “for” attribute of the LABEL tag, all browsers have this functionality already built-in. Kind of a useless post, really.
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.