HTML5 placeholder disappears on focus
Stefano J. Attardi wrote a nice jQuery plugin that just does that.
It is more stable than Robert's and also fades to a lighter grey when the field gets focused.
- See the demo page
- Grab it on GitHub
- Play with the fiddle
I modified his plugin to read placeholder
attribute as opposed to manually creating a span
.
This fiddle has complete code:
HTML
<input type="text" placeholder="Hello, world!">
JS
// Original code by Stefano J. Attardi, MIT license
(function($) {
function toggleLabel() {
var input = $(this);
if (!input.parent().hasClass('placeholder')) {
var label = $('<label>').addClass('placeholder');
input.wrap(label);
var span = $('<span>');
span.text(input.attr('placeholder'))
input.removeAttr('placeholder');
span.insertBefore(input);
}
setTimeout(function() {
var def = input.attr('title');
if (!input.val() || (input.val() == def)) {
input.prev('span').css('visibility', '');
if (def) {
var dummy = $('<label></label>').text(def).css('visibility','hidden').appendTo('body');
input.prev('span').css('margin-left', dummy.width() + 3 + 'px');
dummy.remove();
}
} else {
input.prev('span').css('visibility', 'hidden');
}
}, 0);
};
function resetField() {
var def = $(this).attr('title');
if (!$(this).val() || ($(this).val() == def)) {
$(this).val(def);
$(this).prev('span').css('visibility', '');
}
};
var fields = $('input, textarea');
fields.live('mouseup', toggleLabel); // needed for IE reset icon [X]
fields.live('keydown', toggleLabel);
fields.live('paste', toggleLabel);
fields.live('focusin', function() {
$(this).prev('span').css('color', '#ccc');
});
fields.live('focusout', function() {
$(this).prev('span').css('color', '#999');
});
$(function() {
$('input[placeholder], textarea[placeholder]').each(
function() { toggleLabel.call(this); }
);
});
})(jQuery);
CSS
.placeholder {
background: white;
float: left;
clear: both;
}
.placeholder span {
position: absolute;
padding: 5px;
margin-left: 3px;
color: #999;
}
.placeholder input, .placeholder textarea {
position: relative;
margin: 0;
border-width: 1px;
padding: 6px;
background: transparent;
font: inherit;
}
/* Hack to remove Safari's extra padding. Remove if you don't care about pixel-perfection. */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.placeholder input, .placeholder textarea { padding: 4px; }
}
How do I auto-hide placeholder text upon focus using css or jquery?
<input
type="text"
placeholder="enter your text"
onfocus="this.placeholder = ''"
onblur="this.placeholder = 'enter your text'" />
Placeholder doesn't disappear upon focus in Chrome
Firefox and chrome(and safari) act different on HTML5 placeholders. If you want chrome to disappear the placeholders on focus, you can use following script:
$('input:text, textarea').each(function(){
var $this = $(this);
$this.data('placeholder', $this.attr('placeholder'))
.focus(function(){$this.removeAttr('placeholder');})
.blur(function(){$this.attr('placeholder', $this.data('placeholder'));});
});
make a Placeholder not disappear when typing
Put the equation and input in a container, then style the container to look like an input and the input to look normal.
.captcha {
display: inline-block;
padding: 0.2em;
background-color: white;
border: 1px solid #A9A9A9;
}
.placeholder {
color: #A9A9A9;
}
.answer {
padding: 0;
background-color: transparent;
border: none;
outline: none;
}
<div class="captcha">
<label class="placeholder" for="answer-1">24 - 3 + 6 = </label>
<input id="answer-1" class="answer" type="number" name="answer-1">
</div>
<div class="captcha">
<label class="placeholder" for="answer-2">(24 - 3 + 6) * (1579 - 41 + 2) = </label>
<input id="answer-2" class="answer" type="number" name="answer-2">
</div>
How to move placeholder to top on focus AND while typing?
You could do it like this
HTML:
<div>
<input type="text" class="inputText" />
<span class="floating-label">Your email address</span>
</div>
CSS:
input:focus ~ .floating-label,
input:not(:focus):valid ~ .floating-label{
top: 8px;
bottom: 10px;
left: 20px;
font-size: 11px;
opacity: 1;
}
.inputText {
font-size: 14px;
width: 200px;
height: 35px;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top: 18px;
transition: 0.2s ease all;
}
Working JSFiddle here https://jsfiddle.net/273ntk5s/2/
Related Topics
How to Save the Content in Uiwebview for Faster Loading on Next Launch
How to Make Last Cell of a Row in a Table Occupy All Remaining Width
@Font-Face Not Working on Mobile
How to Strike Through Obliquely with CSS
Can Auto Margins Work in CSS Grid Like They Do in Flexbox
How to Avoid Wrapping in CSS Float
CSS 2 Div Size Auto Same Height
Float Multiple Fixed-Width/Varible-Height Boxes into 2 Columns
How to Center-Align One Flex Item and Right-Align Another Using Flexbox
Chrome Could Play HTML5 Mp4 Video But HTML5Test Said Chrome Did Not Support Mp4 Video Codec
Restoring the Value of a Input Type=File After Failed Validation
How to Move Placeholder to Top on Focus and While Typing