Skip to content

Instantly share code, notes, and snippets.

@yono
Created February 26, 2011 04:12
Show Gist options
  • Save yono/844932 to your computer and use it in GitHub Desktop.
Save yono/844932 to your computer and use it in GitHub Desktop.
空の入力フォームにヒントメッセージを表示する JavaScript
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
<!--
$(function(){
$("input.input-hint").fillHint();
});
jQuery.fn.extend({
fillHint: function() {
var $$ = $(this);
var defaultWord = $(this).attr('title');
$(this)
.focus( function(){
if( $.trim($(this).val()) == defaultWord ){
$(this).css('color', '#000')
.val('');
}
});
$(this)
.blur( function(){
if( $.trim($(this).val()) == defaultWord || $.trim($(this).val()) == '') {
$(this).css('color', '#999')
.val(defaultWord)
}
});
$(this).parents('form:first')
.submit(function(){
if ($.trim($$.val()) == defaultWord) {
$$.triggerHandler('focus');
}
}).end()
.blur();
return $(this);
}
});
-->
</script>
</head>
<body>
<form action="" method="POST" id="form">
  検索: <input type="text" size="30" name="hoge" class="input-hint" title="検索キーワードを入力してください" value=""/><br/>
<input type="submit" value="検索"/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment