jQuery Form Highlighting

jQuery Form Highlighting

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on September 20, 2009

Carrying on the theme from yesterday with the jQuery & PHP username availability checker, i’ve got a quick tip to improve the user experience, so the user knows exactly where they are in the filling out complex forms. We could use CSS to do this alone, however it doesn’t work in Internet Explorer. So in order to cater for all browsers we can achieve the same effect with JavaScript.

The HTML

So in the code below we simply have a form input field wrapped in a div with the class ‘input’. The basic logic of how the code works is that when a user clicks inside the input field it triggers an event listener which triggers a function, which adds or removes a class to the parent element in relation to the form input field. Sounds quite complex when you read it but its simple. Lets break it down.

  1. Below we have 3 form input fields
  2. Notice that each form element is wrapped in a div with the same class
  <div class='input'> Name:<br/>
    <input name='Name' type='text'/>
  </div>
  <div class='input'> Email:<br/>
    <input name='Email' type='text'/>
  </div>
  <div class='input'> Town:<br/>
    <input name='Email' type='text'/>
  </div>

The JavaScript

  1. We start with our first function which is in our DOM ready function. This function is run when a user clicks, or tabs into the input field. You can see that all we do is add the class ‘highlight’ to the div class ‘input’
    	$('input').focus(function() {
    			$(this)
    				.parent().addClass('highlight')
    		});
    
  2. We do the same with this function but instead of it adding the class highlight we simply remove it.
    	$('input').blur(function() {
    			$(this)
    				.parent().removeClass('highlight')
    		});
    

The Complete JavaScript

$(document).ready(function(){
		$('input').focus(function() {
			$(this)
				.parent().addClass('highlight')
		});

		$('input').blur(function(){
			$(this)
				.parent().removeClass('highlight')
		});

	});

A Sprinkle Of Style

.input {
	padding:6px;
	width:315px;
	border: 1px solid #fff;
}
input {
	border:4px solid #666;
	padding:4px;
	width:300px;
}
.highlight {
	background-color:#FF6;
	border: 1px solid #FC6;
}

demodownload


Recent shares

More tutorials from Papermashup
Comments
4 discussions around jQuery Form Highlighting
  1. ponsfrilus says:

    Nice tut but labels, textarea and submit button are missing.

    • Ashley says:

      @ponsfrilus i need to update the labels in the code, I still haven’t got around to that. Completely baffled by the textarea and submit button being missing… works fine for me.

  2. Webdeveloper says:

    Nice article! Really like the demo, shows immediately how usefull the highlighting can be!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.