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

  • Home
  • SEARCH
  • 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 493731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:25:59+00:00 2026-05-13T05:25:59+00:00

I am trying to create a custom jQuery selector that will put focus() on

  • 0

I am trying to create a custom jQuery selector that will put focus() on a input text field whose label text is provided by the user.

It is simple, I take the text provided by the user, search for the text node of all labels, get the matching label’s ‘for’ attribute value and use that as an id to get the input element.

My problem is that I am unable to access the text node as the nodes being passed in stack are everything except the text nodes. I checked the child element of the label and it says null in firebug.

Here is what part of the code looks like:

$.expr[':'].findipfromlabel = function(obj, index, meta, stack) {
 if($(obj).find("label").text() == meta[3]) {
  var forattr = $(obj).attr('for');
 }
 return ($(input).attr('id' == forattr));
};

$(document).ready(function() {
 $(': findipfromlabel(Male)').focus();
});

In above, label elements are present but there is no child element of that label. There is a next sibling, but how to confirm with label to use unless I know the text node value equals what the user has entered?
HTML is:

<form>
  <label for="v">test</label>
  <input type="text" name="v" id="v" /><br/>
  <label for="male">Male</label>
  <input type="text" name="m" id="male" />
  <br />
  <label for="fe">Female</label>
  <input type="text" name="f" id="fe" />
</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-13T05:25:59+00:00Added an answer on May 13, 2026 at 5:25 am

    I see two one problems: First, the “forattr” variable is being defined inside the scope of the if block, so it isn’t available to your return statement. Second, Your return statement is returning the value of the “true” attribute of the last input on the page, which is probably not what you want.

    Try something like this:

    $.expr[':'].findipfromlabel = function(obj, index, meta, stack)
    {
      if($(obj).find("label").text() == meta[3])
      {
        return $('input#' + $(obj).attr('for'));
      }
      return null;
    };
    

    EDIT: Removed incorrect assumption regarding JavaScript scoping rules.

    EDIT: Really works this time:

    Okay, I think I have it figured out now for sure. This would be obvious for an expert on custom expressions, but I actually just learned how to write them now. The key is you should simply return true if “obj” is the node you’re looking for (i.e. the input), or false if it is not. This works with my test page:

    $.expr[':'].findipfromlabel = function(obj, index, meta, stack)
    {
      var label = $('label').filter( function() { return $(this).text() == meta[3]; } );
    
      if( label.length > 0 )
      {
        return $(obj).attr('id') == label.attr('for');
      }
    
      return false;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: Dim date As Datetime = DateTime.ParseExact(_ yourString, "dd/MM/yyyy",… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer Here's an extension which makes it work (at least partially):… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer Because in the case of the varchar, it has to… May 14, 2026 at 8:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.