A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » jQuery » Trigger Click on Input when Label is Clicked Submit one!

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');
});

Subscribe to The Thread

  1. TeMc says:

    Not sure if that’s what you mean, but Safari does this built-in.

  2. Bill Brown says:

    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>

  3. TerranRich says:

    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.

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---