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.
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.
<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>
$('input').focus(function() {
$(this)
.parent().addClass('highlight')
});
$('input').blur(function() {
$(this)
.parent().removeClass('highlight')
});
$(document).ready(function(){
$('input').focus(function() {
$(this)
.parent().addClass('highlight')
});
$('input').blur(function(){
$(this)
.parent().removeClass('highlight')
});
});
.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;
}
Nice tut but labels, textarea and submit button are missing.
Nice article! Really like the demo, shows immediately how usefull the highlighting can be!
Use labels.
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
4 discussions around jQuery Form Highlighting