Skip to content

Instantly share code, notes, and snippets.

@ChoiZ
Last active December 10, 2015 19:59
Show Gist options
  • Select an option

  • Save ChoiZ/4485453 to your computer and use it in GitHub Desktop.

Select an option

Save ChoiZ/4485453 to your computer and use it in GitHub Desktop.
Create a placeholder with jQuery and xhtml
.placeholder {
color:#ccc;
}
<!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>
$(".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