Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jQuery .blur() form problem...help!

Featured Replies

Hey guys,

 

I'm either having a dumb moment here, or I just can't find the answer, but I've written a script using jQuery that takes the elements of my form, checks to see if the default value is currently displayed on focus, if so the field is set to blank.

 

My problem is on blur. I want to loop through my default value array, find the name that corresponds to the input name, and add the default value to the input field.

 

Hopefully this makes sense to you guys, I've attached the code below:

 

 

var defaults = { 
'name' : 'Enter your name..',
'email' : 'Your e-mail address so I can reply..',
'website' : 'Your existing website (optional)',
'subject' : 'The subject of your message..',
'message' : 'Your message..'
};

$("input, select, textarea").focus(function() {

var val = $(this);

$.each(defaults, function(key, value) { 
	if (val.val() == value || val.val() == "") {
		val.val("");
		return false;
	}
});
});
$("input, select, textarea").blur(function() {

var val = $(this);
$.each(defaults, function(key, value) { 
	if (val.val() == "") {
		//what do i write?!
	}
});
});

 

Any help is appreciated.

Umm...to simplify the whole thing;

 

$(document).ready(function(){
       $("input, select, textarea").focus(function() {
           if (this.value == this.defaultValue)
           {
               this.value = "";
           }
       });
       $("input, select, textarea").blur(function() {
           if (this.value == "")
           {
               this.value = this.defaultValue;
           }
       });
   });

 

Replace all the code you posted above with that and you should be set to go. Forgive me if it's buggy - it's late :blush1:

  • Author

where does defaultValue come from?

 

Can I just write..

 

<input type="text" name="name" value="Enter your name.." defaultValue="Enter your name.." />

 

Thanks

where does defaultValue come from?

 

Can I just write..

 

<input type="text" name="name" value="Enter your name.." defaultValue="Enter your name.." />

 

Thanks

defaultValue is the original value of the value attribute. You don't need to add it, just put something in the value attribute

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.