Last active
December 10, 2015 03:38
-
-
Save usmans/4375681 to your computer and use it in GitHub Desktop.
Changing Form Input Styles on Focus with jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://buildinternet.com/2009/01/changing-form-input-styles-on-focus-with-jquery/ | |
<form> | |
<input name="status" id="status" value="Type something here" type="text"/> | |
<input value="Submit" type="submit"/> | |
</form> | |
<style type="text/css"> | |
*{ | |
margin:0; | |
padding:0; | |
font:bold 12px "Lucida Grande", Arial, sans-serif; | |
} | |
body { | |
padding: 10px; | |
} | |
#status{ | |
width:50%; | |
padding:10px; | |
outline:none; | |
height:36px; | |
} | |
.focusField{ | |
border:solid 2px #73A6FF; | |
background:#EFF5FF; | |
color:#000; | |
} | |
.idleField{ | |
background:#EEE; | |
color: #6F6F6F; | |
border: solid 2px #DFDFDF; | |
} | |
</style> | |
$(document).ready(function() { | |
$('input[type="text"]').addClass("idleField"); | |
$('input[type="text"]').focus(function() { | |
$(this).removeClass("idleField").addClass("focusField"); | |
if (this.value == this.defaultValue){ | |
this.value = ''; | |
} | |
if(this.value != this.defaultValue){ | |
this.select(); | |
} | |
}); | |
$('input[type="text"]').blur(function() { | |
$(this).removeClass("focusField").addClass("idleField"); | |
if ($.trim(this.value == '')){ | |
this.value = (this.defaultValue ? this.defaultValue : ''); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment