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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:01:45+00:00 2026-06-06T20:01:45+00:00

I have 4 textboxes on my page. I used jquery focus and blur functions

  • 0

I have 4 textboxes on my page. I used jquery focus and blur functions to make the default of the textbox to appear and disappear. My code:

$(document).ready(function() {

$("#name").focus(function() {
    $("#name").removeClass("idleField").addClass("focusField");
    if($("#name").val() == "username") {
        $("#name").val('');
    }
});

$("#name").blur(function() {
    $("#name").removeClass("focusField").addClass("idleField");
    if($("#name").val().length == 0) {
        $("#name").val('username');
    }
});

$("#password").focus(function() {
    $("#name").removeClass("idleField").addClass("focusField");
    if($("#password").val() == "password") {
        $("#password").val('');
    }   
});

$("#password").blur(function() {
    $("#name").removeClass("focusField").addClass("idleField");
    if($("#password").val().length == 0) {
        $("#password").val('password');
    }
});

$("#firstname").focus(function() {
    $("#firstname").removeClass("idleField").addClass("focusField");
    if($("#firstname").val() == "firstname") {
        $("#firstname").val('');
    }
});

$("#firstname").blur(function() {
    $("#firstname").removeClass("focusField").addClass("idleField");
    if($("#firstname").val().length == 0) {
        $("#firstname").val('firstname');
    }
});

$("#lastname").focus(function() {
    $("#lastname").removeClass("idleField").addClass("focusField");
    if($("#lastname").val() == "lastname") {
        $("#lastname").val('');
    }   
});

$("#lastname").blur(function() {
    $("#lastname").removeClass("focusField").addClass("idleField");
    if($("#lastname").val().length == 0) {
        $("#lastname").val('lastname');
    }
});

$("#email").focus(function() {
    $("#email").removeClass("idleField").addClass("focusField");
    if($("#email").val() == "username@example.com") {
        $("#email").val('');
    }
});

$("#email").blur(function() {
    $("#email").removeClass("focusField").addClass("idleField");
    if($("#email").val().length == 0) {
        $("#email").val('username@example.com');
    }
});

});

am i doing any thing wrong here?

Any help would be apreciated!

  • 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-06-06T20:01:47+00:00Added an answer on June 6, 2026 at 8:01 pm

    There is a mistake in your #password field, where you still have #name as the selector.

    Also, it would be better to handle this differently, I would for example, store the original values as data attributes of each element that requires one, and then attach an event handler to all of them, which checks the element’s data attribute against it’s current value…

    Assuming HTML something like this…

    <input type='text' id='name' data-init='first name'>
    <input type='text' id='age' data-init='your age'>
    

    jQuery something like this works…

    // select all the elements to operate on
    $("#name,#age").each(function(){
        $me = $(this) // cache the current jQuery object for better performance
        $me.val($me.data('init')) // set the val to the data object of each element
    }).focus(function(){ // handle focus for all elements
        $me = $(this)
        $me.removeClass('idleField').addClass('focusField')
        if($me.val() == $me.data('init'))
            $me.val('')
    }).blur(function(){ // handle blur for all emements
        $me = $(this)
        $me.removeClass('focusField').addClass('idleField')
        if($me.val().length == 0)
            $me.val($me.data('init'))
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET web page that contains many textboxes. Each textbox has a
I have a standard textbox and I've got jQuery on the page. I want
I have used jquery datepicker in my .aspx page. The control is working fine.
I have a textbox on my web page that is used to specify a
I have used DependencyProperty to pass value from textbox at silverlight page to external
I have some textboxes on a page and I want to click a link
I have taken three textboxes over aspx page. A JavaScript function is associated with
I have multiple input textboxes in my page. I want to reset particular text
I have a bunch of textboxes on a page and a submit button. I
I have a simple aspx page with a few TextBoxes and a submit button.

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.