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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:40:14+00:00 2026-05-25T17:40:14+00:00

I have multiple textboxes with the same class .input. Each textbox has different default

  • 0

I have multiple textboxes with the same class “.input”. Each textbox has different default text: First Name, Last Name, etc…

On the focus of a particular textbox, I’d like the default text to go away, and a “.focus” class to be added to the textbox. On the blur of that textbox, I’d like the default text to return if no text has been entered by the user. I’d also like the “.focus” class to be removed unless the user has entered text.

I know how to do this for one textbox, but not multiple (without writing a function for each textbox). I’m guessing there is a way.

I’m also working with asp.NET textboxes so I had to modify the selector.

var myText
var myBox
$('input:text.input').focus(function () {
    myText = $(this).val();
    myBox = $(this);
    //alert('focus');
}).blur(function () {
    alert($(myText).val());
});

I’m pretty sure the global vars don’t retain their values by the time the blur function is called. I’ve seen this done many times on multiple sites, I just can’t figure it out.

Any help is appreciated. Thanks!

I’ve searched and come up with something close… However, when blur is called, the default text goes away even though I’ve entered in text.

        $('input:text.input').focus(function () {

        if (this.value == this.defaultValue) {
            this.value = '';
        }
        if (this.value != this.defaultValue) {
            this.select();
        }
    });
    $('input:text.input').blur(function () {
        $(this).removeClass("focus");
        if ($.trim(this.value == '')) {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });

Thanks for your help! Here is the code with some tweaks.

    $('input:text.input').focus(function () {
       if($(this).hasClass('focus')) { } else {
         $(this)
           .data('stashed-value', $(this).val()) // stash it
           .addClass('focus')
           .val(''); // clear the box
        }
    }).blur(function () {
       if ($(this).val() != '') { } else {
         $(this)
           .val($(this).data('stashed-value')) // retrieve it
           .removeClass('focus');
        }
});
  • 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-25T17:40:15+00:00Added an answer on May 25, 2026 at 5:40 pm

    One way is to stash the value you want to save within the element itself, using data():

    $('input:text.input').focus(function () {
        $(this)
          .data('stashed-value', $(this).val() ) // stash it
          .val(''); // clear the box
    }).blur(function () {
        // presumably you'll check some conditions here (e.g. input empty) then:
        $(this).val( $(this).data('stashed-value') ); // retrieve it
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple input textboxes in my page. I want to reset particular text
In my form, I have multiple fields (hidden text boxes) which has the same
If I have multiple textboxes which have different ID's but all id's start with
I have a XAML window with multiple TextBoxes, each with a corresponding TextBlock tag
I have a web form that has multiple ListBoxes, TextBoxes, DropDowns. If I put
I have a Custom Control that has multiple textbox fields and a checkbox contained
I have multiple textboxes which I want them to perform the same thing upon
I have a form with multiple textboxes with the same names. I want to
I have an WPF usercontrol that has a TextBox. I set the text Underline
I have multiple textboxes bound to different data, but I would like every one

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.