Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6069799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:51:42+00:00 2026-05-23T09:51:42+00:00

Short Question: How do you link a label element to an input element without

  • 0

Short Question:
How do you link a label element to an input element without using the input element’s id using jQuery and javascript?

Long Question:
I am using jQuery to clone a form with possibly more than one instance of the form being available for the user to fill in.

A label’s ‘for’ attribute is supposed to be set to the ‘id’ attribute of the input element that it is for. This works when the input element has a unique id.

Because I am cloning the same input element there will be multiple input elements with the same id in the document. Therefore I’m avoiding having id attributes for input elements but I’d still like to focus on the input element when the label is clicked. I also want to avoid generating random ids for fields or setting onclick events on labels.

Edit #1

Example mark up (note no ids)

<form>    
<label>First Name:</label><input type='text' name='FirstName' /><br/>
<label>Last Name:</label><input type='text' name='LastName' /><br/>
</form>

Example cloning code:

var newForm = $('form').clone();

$(newForm).find('label').each(function(){
   var inputElement = $(this).next('input');

   // I'd love to set the label's for attribute to an element
   $(this).attr('for', inputElement);

});

$(document).append(newForm);

Edit #2

There currently are three options:

  • Set onclick events for labels to focus on the input field they’re for. Criteria for deciding which labels are for which inputs can be the next input element or something else
  • Embed the input fields in the label fields (might not be possible due to designer’s choices)
  • Generate random ids while cloning each form
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:51:42+00:00Added an answer on May 23, 2026 at 9:51 am

    Well it would be nice to see the markup, but if i can assume that the markup will look somewhat like this

    <form name="f1">
    <label>this is my label</label>
    <input />
    <label>this is my other label</label>
    <input />
    </form>
    
    <form name="f2">
    <label>this is my label</label>
    <input />
    <label>this is my other label</label>
    <input />
    </form>
    

    then you could do something like this

    $('form label').live('click',function(){
      $(this).next('input').focus();
    });
    

    you will need to use live or delegate since you’re cloning the forms on the fly i’m assuming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
Long story short, I'm using a buggy WordPress template and we're too far into
Short question: What does an exception's sourceID refer to, and how can I link
I am trying to teach myself AI using neural networks. Long story short, I
Short question - how do you define your view models? Here are some of
Short question: Is it possible to do a redirection, say when a user isn't
Short question: Is the parent of an entity group included in that entity group
Short question. How do you go about transposing UML diagrams into code? The class
A short question: I've got a TabNavigator with multiple canvas children. How can i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.