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 4028458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:13:21+00:00 2026-05-20T11:13:21+00:00

I have some inputs with floating labels over the top of them. The effect

  • 0

I have some inputs with floating labels over the top of them. The effect is basically, inside an email text input, it says ‘Enter your email’. As you click the input, the label fades away.

I have a working function for it all, but I have some repeating code.

IE:

My html looks like this:

 <div id="action">

   <form action="/" method="post" id="#register_form">


      <input type="hidden" name="create" value="1" />

      <div class="form-field">
        <label for="register_name">Name (First &amp; Last)</label>
        <input id="register_name" class="form-text" type="text" name="name" tabindex="1"/>
        </div>


    <div class="form-field">
        <label for="register_email">Enter your email</label>
        <input id="register_email" class="form-text" type="text" name="email"  tabindex="2"/>
    </div>

    <div class="form-field">
      <label for="register_password">Password</label>
      <input id="register_password" class="form-password" type="password" name="password" tabindex="3"/>
    </div>

    <button type="submit">Almost there!</button>

  </form>

</div>

And the jquery looks like so:

$('#action input[type=text], #action input[type=password]').focus(function(){

    // get this label
    var this_id = $(this).attr('id');

    $('label[for='+this_id+']').addClass('in_lighter');


}).blur(function(){

    // get this label
    var this_id = $(this).attr('id');

    if($(this).val()==""){
        $('label[for='+this_id+']').removeClass('in_lighter');
    }

}).keyup(function(){


    // get this label
    var this_id = $(this).attr('id');

    $('label[for='+this_id+']').fadeOut();


});

As you can see, the function does a few different things depending on whether its a focus, blur, or keyup. In each of those functions though, it grabs the id the selected input, then uses that to identify the label to work with. As you can see, I’m repeating this code 3 times:

    // get this label
    var this_id = $(this).attr('id');

Is there a better approach to this? A more streamlined method to achieve the same result?

  • 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-20T11:13:22+00:00Added an answer on May 20, 2026 at 11:13 am

    Instead of:

    var this_id = $(this).attr('id');
    $('label[for='+this_id+']').addClass('in_lighter');
    

    You could do:

    $(this).prev("label").addClass('in_lighter');
    

    This would get the next previous element which was a label, so you wouldn’t need any id hookup then.

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

Sidebar

Related Questions

I have some hidden inputs like this <input name=exam.normals[1].blahblah ..../> I would like somehow
I have a View where some input text to be added dynamica using jquery,
I have some checkbox inputs like so: <input type=checkbox name=1 class=filter/> <input type=checkbox name=2
I have some jquery that inputs text into a database and then clears the
I have some HTML text inputs into a WebView, and I need to disable
I have some inputs with OnBlur event handlers something like <input name=abc tabIndex=5 class=datetime
If I have some hidden inputs in my form: <input type=hidden name=test value=somedata> <input
I have some labels and attributes from text. I am looking for patterns (combinations
I have some inputs on my site representing floating point numbers with up to
I have some disabled inputs in a form and I want to send them

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.