Last active
December 10, 2015 19:59
-
-
Save ChoiZ/4485453 to your computer and use it in GitHub Desktop.
Create a placeholder with jQuery and xhtml
This file contains hidden or 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
| .placeholder { | |
| color:#ccc; | |
| } |
This file contains hidden or 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
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
| <head> | |
| <link rel="stylesheet" type="text/css" href="placeholder.css" /> | |
| <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
| <script type="text/javascript" src="placeholder.js"></script> | |
| <title>Placeholder with jQuery and xhtml</title> | |
| </head> | |
| <body> | |
| <form method="post" action=""> | |
| <input type="text" class="text_placeholder" data-placeholder="Search" name="search" /> | |
| <input type="submit" value="Submit" /> | |
| </form> | |
| </body> | |
| </html> |
This file contains hidden or 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
| $(".text_placeholder").focus(function() { | |
| if( $(this).val() == $(this).attr('data-placeholder') ) { | |
| $(this).removeClass('placeholder').val(''); | |
| } | |
| }).blur(function() { | |
| if( $(this).val() == $(this).attr('data-placeholder') || $(this).val() == '' ) { | |
| $(this).addClass('placeholder').val($(this).attr('data-placeholder')); | |
| } | |
| }).blur(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment