PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Clearing default field values with Jquery

  1. jblack350

    I am having a little trouble getting the following tutorial to work on my wordpress theme:

    The form is on the sidebar for email signup:

    I have added the code snippet to my header file in the head tags and also changed the css class name to "clearit" on the form. No luck though. How can I tell if the jQuery library is loaded in my theme? If it is not, are there tutorials on how to do this?


    Posted 9 years ago on Sunday August 26, 2012 | Permalink
  2. The problem is you loaded the clearit script before jQuery is loaded, so this will never work.


    You need to include this script:

    <script type="text/javascript">
    jQuery(document).ready(function() {
    	jQuery.fn.cleardefault = function() {
    	return this.focus(function() {
    		if( this.value == this.defaultValue ) {
    			this.value = "";
    	}).blur(function() {
    		if( !this.value.length ) {
    			this.value = this.defaultValue;
    jQuery(".clearit input, .clearit textarea").cleardefault();

    after jQuery is loaded by the theme. How did you add that script? If you just pasted it into header.php, you put it up too high. It need to be closer to the closing </head> tag, like right before it actually. And it certainly needs to be after jQuery is loaded (in other words, your clearit script needs to come after line 41 in the screenshot.

    That should get it working for you at least. However, the proper way to do this is to enqueue this script, only on the pages where you need it (maybe all, since it's a sidebar form) and make sure its dependency of jquery is satisfied before loading. We can tackle that, if you want, after this is working with the code pasted into the correct place in header.php. Thank you.

    Posted 9 years ago on Monday August 27, 2012 | Permalink