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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:15:10+00:00 2026-05-25T23:15:10+00:00

I have a simple script in jQuery that works perfectly with jQuery 1.5.2 as

  • 0

I have a simple script in jQuery that works perfectly with jQuery 1.5.2 as you can see in this jsFiddle. What is supposed to happen is that when you bring focus to the text field, the default value is removed. And when if you leave the field blank, the original default value is put back in place.

http://jsfiddle.net/kHBsD/

However, the same exact code, where only jQuery 1.6.3 is used instead, is not working. (Not working means that the default value remains in the text box until you manually delete it as you can see in this jsFiddle.

http://jsfiddle.net/kHBsD/1/

There are no script errors in the console and other aspects of the function are operational. You can see the hover() portion is working fine in both jsFiddles.


Summarized Version (the Root Problem)

jQuery 1.6.3 is returning undefined for .attr('defaultValue').

jsFiddle using jQuery 1.6.3 (not working)

However, jQuery 1.5.2 is returning the expected value for .attr('defaultValue').

jsFiddle using jQuery 1.5.2 (working)


Question:

Does anyone know why this would be happening? (It looks like a jQuery bug to me.)

The following is still working…

document.getElementById().defaultValue

…but I think it’s pretty ugly to have to do that where jQuery is available.

I’m open to other suggestions.

  • 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-25T23:15:11+00:00Added an answer on May 25, 2026 at 11:15 pm

    Use prop():

    $( '#q' ).prop( 'defaultValue' )
    

    Live demo: http://jsfiddle.net/kHBsD/8/

    You see, 'defaultValue' is not a content attribute (HTML attribute) as you can see for yourself if you look at your HTML source code. Instead, it’s a property of the HTMLInputElement DOM element node.

    See here: https://developer.mozilla.org/en/DOM/HTMLInputElement


    Attributes exist in the HTML source code.
    Properties exist in the DOM tree.

    When the browser parses the HTML source code, the HTML <input> element is interpreted and a corresponding HTMLInputElement DOM node is created. This DOM element contains dozens of properties ('defaultValue' being one of them).


    Here, I’ve refactored your code:

    $( '.autoclear' ).
        focus( function () {
            if ( this.value === this.defaultValue ) {
                this.value = '';
                $( this ).removeClass( 'blur' ).addClass( 'focus' );
            }
        }).
        blur( function () {
            if ( this.value === '' ) { 
                this.value = this.defaultValue;
                $( this ).removeClass( 'focus' ).addClass( 'blur' );
            }
        });
    

    Live demo: http://jsfiddle.net/kHBsD/9/

    prop() is not necessary – you have the this reference, so you can just access the property directly. Also those hover handlers are not needed, just use a input:hover CSS rule.

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

Sidebar

Related Questions

I have a simple show/hide jquery script that works fine on its own, The
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have a simple script that does some search and replace. This is basically
I have written a simple jQuery script that changes the hash tag of a
I have this simple script that catches all mouse clicks, unless you click on
I have a personal simple jQuery validation script. It works when the submit button
I have a simple file that has a Jquery script it in that looks
This simple script, to add / delete tablerows using jQuery works well with version
I have a simple load more style script that works fine on the index
I have a very simple jQuery script.. all I want it to do is

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.