NVDA Reading out Text Twice in Chrome - html

For the following markup:
<div class="form-group">
<label class="control-label" for="email">Email Address</label>
<input type="text" name="email" id="email" placeholder="Email Address" class="form-control">
</div>
this is what is being readout by NVDA:
Firefox
Email Address Edit Blank
IE
Email Address Edit Blank
Chrome
Email Address Edit Email Address Blank
It seems that chrome is also reading out the placeholder text but Firefox and IE aren't. Removing placeholder text isn't an option since it is a requirement.
In this case, is there a way I can make Chrome not read the placeholder text?

<input aria-hidden="true" role="alert" class="range from hasDatepicker" type="text" value="" data-type="range" id="range-from" data-uniq-id="11/19/2020" name="range-from" aria-label="11/19/2020">
The solution to double-reading in the example above for me was to add aria-hidden = "true" great idea and it works.

Ok, so rather than manipulating your prefectly standards compliant HTML, I would simply understand the tools you're working with better. I've tried so many js hacks, but that's just what they are (hacks). Chrome tends to simply read the placeholder text. That's just it. Here are a couple references to check out. They are incredibly helpful.
browser/screenreader combos
aria support breakdown
However, if you REALLY wanted to fix this issue in Chrome, you would detect Chrome/webit (via How to detect chrome and safari browser (webkit))
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
Then you could do one of these options:
REMOVE the placeholder text
REPLACE it with regular text, which would be appended/prepended directly after/before the input, you would format this in css to overlay on the input, use js to set aria-hidden="true" and then hide the text on input focus
Here's a plnkr to show how to do it: plnkr
****NOTE****
That plnkr is sloppy code. I would write a module to accept parameters so it can be used on any input.

This is kind of a late response to this question, but the better solution to prevent the text from being read out loud multiple times would likely be to not use the placeholder at all, but maybe dynamically position the label within the textbox like a placeholder.
$('#form').find('input').on('keyup blur focus', function(e) {
var $this = $(this),
label = $this.prev('label'),
val = $this.val();
if (e.type === 'keyup') {
if (val === '') {
label.removeClass('active highlight');
label.addClass('focused');
} else {
label.addClass('active highlight');
label.removeClass('focused');
}
} else if (e.type === 'blur') {
if (val === '') {
label.removeClass('active highlight focused');
} else {
label.removeClass('highlight focused');
}
} else if (e.type === 'focus') {
if (val === '') {
label.removeClass('highlight');
label.addClass('focused');
} else if (val !== '') {
label.addClass('highlight');
label.removeClass('focused');
}
}
});
#form {
padding: 40px;
width: 300px;
height: 75px;
margin: 40px 40px;
float: left;
}
#form .field-wrap {
position: relative;
margin-bottom: 40px;
}
#form .field-wrap label {
position: absolute;
transform: translateY(6px);
left: 13px;
transition: all 0.25s ease;
-webkit-backface-visibility: hidden;
pointer-events: none;
font-size: 22px;
}
#form .field-wrap label.active {
transform: translateY(-20px);
left: 2px;
font-size: 14px;
}
#form .field-wrap input {
font-size: 22px;
display: block;
width: 100%;
height: 100%;
padding: 5px 10px;
background: none;
background-image: none;
border-radius: 0;
transition: border-color .25s ease, box-shadow .25s ease;
}
#form .field-wrap input:focus {
outline: 0;
}
#form .field-wrap p.context {
font-size: 1em;
margin: .5rem 0rem 0rem 0rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="form">
<div class="field-wrap">
<label for="textField">Label Placeholder Text</label>
<input type="text" name="textField" id="textField" autocomplete="off" aria-describedby="textFieldDescription" />
<p id="textFieldDescription" class="context">Type in some text.</p>
</div>
</div>

Have you tried to delete placeholder on input focus? Something like:
<input type="text" name="email" id="email" placeholder="Email Address"
onfocus="this.placeholder=''"/>
It's not the best solution, but it can work for you.

Related

CSS :selectors animation stops working correctly after removing "required" tag from input field

I have the following code, which move the label out of the input field, as soon as the input is focused or valid.
.form-control-placeholder {
font-family: "Zuric Light";
color: black;
position: absolute;
top: 0;
padding: 7px 0 0 13px;
transition: all 200ms;
opacity: .5;
}
.form-control:focus + .form-control-placeholder,
.form-control:valid + .form-control-placeholder {
padding: 0;
transform: translate3d(0, -100px, 0);
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="form-group">
<input type="text" id="address2" class="form-control" name="address2" required>
<label class="form-control-placeholder" for="address2">Address line 2</label>
</div>
As you might see, the required attribute is applied to the input field. But as soon as I remove it, it stops working correctly because of the :valid selector. Without the required attribute, it will be valid all the time.
How do I solve this and can this be achieved with solely CSS?
:valid works only if the input have required attribute. Other way to achieve this is using :placeholder-shown Pseudo class.
This pseudo class select all input and textarea that currently shows the placeholder text. In order to work, the input element must have the placeholder attribute.
According to MDN, MS Edge won't support this.
In this case, You want the label to go up only when input is focussed or it contains some value. So you need to use :not(:placeholder-shown) instead of :placeholder-shown. Otherwise it'll produce inverse effect.
div{
position: absolute;
width:100%;
top:50px;
}
.form-control-placeholder {
font-family: "Zuric Light";
color: black;
position: absolute;
top: 0;
padding: 7px 0 0 13px;
transition: all 200ms;
opacity: .5;
}
.form-control:focus+.form-control-placeholder,
.form-control:not(:placeholder-shown)+.form-control-placeholder {
padding: 0;
transform: translate3d(0, -100%, 0);
opacity: 1;
}
<div class="form-group">
<input type="text" id="address2" class="form-control" ngModel name="address2" placeholder="...">
<label class="form-control-placeholder" for="address2">Address line 2</label>
</div>

Horizontaly aligning placeholder in input field

What would be correct approach to aligning placeholder to the top of the field, while input text appearing normally in the middle?
Any way to do that with CSS on input/::placeholder only, or should i rather construct a wrapper with span that would disappear when active and input field below it?
Here's a fiddle of what i've got now: https://jsfiddle.net/ejsLfvdn/1/
And that's what it should look like up to customers will:
The input masks are not the case here, i'm only struggling with the placeholder being aligned to the top, while input should appear normally in the middle. The placeholder MUST disappear after filling input.
I don't think that you will be able to do this by directly targeting the placeholder pseudo class (::placeholder).
Only a small subset of CSS properties can be applied to this element and position is not one of them:
https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder
I think you will need to take the approach of a wrapper with span and input and position appropriately.
You could use something like this with the only issue being the input must have the required attribute.
* {
box-sizing: border-box;
}
.input {
display: flex;
flex-flow: column-reverse nowrap;
border: 1px solid gray;
width: 220px;
}
.input input:valid + label {
opacity: 0;
}
.input input {
width: 100%;
padding: 10px;
border: none;
}
<div class="input">
<input required id="username" name="username" type="text" />
<label for="username">Username</label>
</div>
I hope I achieved what you need.
btw, I used jquery to hide the placeholder while typing and display it again if the field is empty.
$('.form-control').keyup(function(){
var val = $(this).val();
if(val == ""){
$('.placeholder').show();
}else{
$('.placeholder').hide();
}
});
.input-cont{
position: relative;
}
.form-control{
border: 1px solid #DDD;
border-radius: 5px;
height: 40px;
padding-left: 8px;
}
.placeholder{
position: absolute;
top: 5px;
left: 8px;
color: #3dc185;
font-size: 12px;
text-transform: uppercase;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<form>
<div class="input-cont">
<span class="placeholder">Imię</span>
<input class="form-control" type="text" name="name">
</div>
</form>
</body>
</html>
You can use translateY(-100%) on your placeholder to move the text upwards and then give your textbox some padding at the top to reveal the text:
.placeholder-offset {
font-size: 20px;
padding-top: 25px;
}
.placeholder-offset::placeholder {
color: red;
transform: translateY(-100%);
}
<input type="text" placeholder="Username" class="placeholder-offset" />

CSS pseudo-element input-placeholder::after to show with or without value in text box

I'm trying to get some UI flagging on form elements for the user after validation to work using the placeholder pseudo elements (starting with text boxes). What I want is the ::input-placeholder::after pseudo element to be shown when there is both a value in the text box as well as when there isn't a value in the text box (e.g. when an invalid value is present or when a value is required - should show the red "X" in the far right of the textbox via the ::input-placeholder::after pseudo-element).
Here's the fiddle:
https://jsfiddle.net/edsinek/L8u7s25d/
Here's a snippet of the CSS I'm using to show the "X" for an invalid value:
.input-invalid input[type=text]::-webkit-input-placeholder::after {
content: "\2716"; // "X"
font-size: 18px;
color: #ff0000;
padding-right: 0;
float: right;
}
Note: I'm only coding for Chrome at the moment, so forgive the webkit specific stuff.
Any ideas? Or is this something that is not possible and is UA dependent?
Original Answer: (doesn't work in Chrome v47 and above+)
When you start typing inside a text box, ::-webkit-input-placeholder element's visibility gets set to hidden. To display its ::after element even when the input box has a value, we need to override it and set the visibility to visible.
.input-invalid input[type=text]::-webkit-input-placeholder::after {
visibility: visible;
}
var addFormFocusEventHandler = function() {
var placeholder;
// using the document syntax in case input & container added to DOM dynamically
$(document).on("focus", "div.input-container :input", function() {
$(this).closest("div.input-container").addClass("input-focused");
placeholder = $(this).prop("placeholder");
$(this).prop("placeholder", " ");
}).on("blur", "div.input-container :input", function() {
$(this).closest("div.input-container").removeClass("input-focused");
$(this).prop("placeholder", placeholder);
placeholder = "";
});
};
var addFormValueEventHandler = function() {
// using the document syntax in case input & container added to DOM dynamically
$(document).on("blur", "div.input-container :input", function() {
if ($(this).val()) {
$(this).closest("div.input-container").addClass("input-has-value");
} else {
$(this).closest("div.input-container").removeClass("input-has-value");
}
});
};
var initialize = function() {
addFormFocusEventHandler();
addFormValueEventHandler();
};
initialize();
label {
color: transparent;
display: block;
}
input[type=text] {
display: block;
border-width: 0 0 1px !important;
border-radius: 0;
border-style: solid;
border-color: rgba(0, 0, 0, 0.12);
}
.input-focused:not(.input-invalid) label {
color: rgb(16, 108, 200);
}
.input-focused:not(.input-invalid) input[type=text] {
border-color: rgb(16, 108, 200);
}
.input-has-value:not(.input-invalid):not(.input-focused) label {
color: #595959;
}
.input-invalid.input-focused label,
.input-invalid.input-has-value label {
color: #ff0000;
}
.input-invalid input[type=text] {
border-color: #ff0000;
}
.input-invalid input[type=text]::-webkit-input-placeholder {
color: #ff0000;
}
.input-invalid input[type=text]::-webkit-input-placeholder::after {
content: "\2716";
/* "X" */
font-size: 18px;
color: #ff0000;
padding-right: 0;
float: right;
}
.input-valid input[type=text]::-webkit-input-placeholder::after {
content: "\2714";
/* checkmark */
font-size: 18px;
color: #438D5B;
padding-right: 0;
float: right;
}
.input-invalid input[type=text]::-webkit-input-placeholder::after {
visibility: visible;
}
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-12 input-container input-required">
<label for="merchantAddress">Merchant Address</label>
<input type="text" class="full-width" id="merchantAddress" placeholder="Merchant Address" />
</div>
<div class="col-xs-12 input-container input-valid">
<label for="merchantCity">Merchant City</label>
<input type="text" class="full-width" id="merchantCity" placeholder="Merchant City" value="" />
</div>
<div class="col-xs-12 input-container input-invalid">
<label for="merchantState">Merchant State Code</label>
<input type="text" class="full-width" id="merchantState" placeholder="Merchant State Code" value="" />
</div>
<div class="col-xs-12 input-container input-invalid input-required">
<label for="merchantZip">Merchant Zip Code</label>
<input type="text" class="full-width" id="merchantZip" placeholder="Merchant Zip Code" value="Bad Data" />
</div>
</div>
</div>
Note: I would not recommend using the placeholder pseudo-element or its child pseudo-element for this purpose but then if you still wish to proceed the above answer would work for you.
+Why does the above not work in Chrome v47 and above?
As pointed out by Pete Talks Web in comments, the above doesn't seem to work in Chrome v47 while it works in Chrome v43. This seems to be because of a key difference in how the placeholder pseudo-element (::-webkit-input-placeholder) is hidden once text has been typed. In v43, the visibility property is used to hide it whereas in v47, it seems like display:none is used. I don't have access to v47 but assume this to be the behavior based on what is observed in Opera which also uses WebKit. An !important is also added to it. This and the fact that the property is added as inline style means it is impossible to override using CSS alone. UA's shadow DOM elements cannot be accessed through JS also and hence there is no way to make the pseudo-element visible once text is typed in.
What happens in the very latest Chrome?
In Chrome v50.0.2638.0 dev-m, the fiddle provided in question itself doesn't work. That is, neither the cross nor the tick marks get displayed. It seems like WebKit has started suppressing the addition of pseudo-elements to the ::-webkit-input-placeholder . This is one thing that I always anticipated happening and it is exactly why I added that note in my answer.
Alternate Solution:
An alternate solution would be to add the pseudo-element to wrapper div and position it as apt. In the below snippet, I have changed input and the pseudo-element to inline-block, positioned the pseudo-element relatively and added a negative margin-left to it. These make it appear near the right edge of the input box. (I had also added width: 100% to the label to make it span the whole line.)
var addFormFocusEventHandler = function() {
var placeholder;
// using the document syntax in case input & container added to DOM dynamically
$(document).on("focus", "div.input-container :input", function() {
$(this).closest("div.input-container").addClass("input-focused");
placeholder = $(this).prop("placeholder");
$(this).prop("placeholder", " ");
}).on("blur", "div.input-container :input", function() {
$(this).closest("div.input-container").removeClass("input-focused");
$(this).prop("placeholder", placeholder);
placeholder = "";
});
};
var addFormValueEventHandler = function() {
// using the document syntax in case input & container added to DOM dynamically
$(document).on("blur", "div.input-container :input", function() {
if ($(this).val()) {
$(this).closest("div.input-container").addClass("input-has-value");
} else {
$(this).closest("div.input-container").removeClass("input-has-value");
}
});
};
var initialize = function() {
addFormFocusEventHandler();
addFormValueEventHandler();
};
initialize();
label {
color: transparent;
display: block;
width: 100%; /* add this */
}
input[type=text] {
display: inline-block; /* modify this */
border-width: 0 0 1px !important;
border-radius: 0;
border-style: solid;
border-color: rgba(0, 0, 0, 0.12);
}
.input-focused:not(.input-invalid) label {
color: rgb(16, 108, 200);
}
.input-focused:not(.input-invalid) input[type=text] {
border-color: rgb(16, 108, 200);
}
.input-has-value:not(.input-invalid):not(.input-focused) label {
color: #595959;
}
.input-invalid.input-focused label,
.input-invalid.input-has-value label {
color: #ff0000;
}
.input-invalid input[type=text] {
border-color: #ff0000;
}
.input-invalid input[type=text]::-webkit-input-placeholder {
color: #ff0000;
}
.input-invalid::after { /* change the selector */
position: relative; /* add this */
display: inline-block; /* add this */
margin-left: -1em; /* add this */
content: "\2716"; /* "X" */
font-size: 18px;
color: #ff0000;
}
.input-valid::after {/* change the seletor */
position: relative; /* add this */
display: inline-block; /* add this */
margin-left: -1em; /* add this */
content: "\2714"; /* checkmark */
font-size: 18px;
color: #438D5B;
}
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-xs-12 input-container input-required">
<label for="merchantAddress">Merchant Address</label>
<input type="text" class="full-width" id="merchantAddress" placeholder="Merchant Address" />
</div>
<div class="col-xs-12 input-container input-valid">
<label for="merchantCity">Merchant City</label>
<input type="text" class="full-width" id="merchantCity" placeholder="Merchant City" value="" />
</div>
<div class="col-xs-12 input-container input-invalid">
<label for="merchantState">Merchant State Code</label>
<input type="text" class="full-width" id="merchantState" placeholder="Merchant State Code" value="" />
</div>
<div class="col-xs-12 input-container input-invalid input-required">
<label for="merchantZip">Merchant Zip Code</label>
<input type="text" class="full-width" id="merchantZip" placeholder="Merchant Zip Code" value="Bad Data" />
</div>
</div>
</div>
Once the user starts typing in the input box, the browser is going to hide the placeholder, and with it, your X.
Unfortunately, you can't place it next to the input with :after, because that is not currently supported.
If you're attached to having the X next to the input, I would suggest placing a div next to input, and adding/showing the X when necessary.
Otherwise, I would move the X like you move the placeholder.
On focus, you set the placeholder to " " and give the label a color. This makes the label visible to the user and keeps a placeholder present, that you X can be :after. You could instead, set the placeholder to "", which would remove your X from the input box, but add an :after or :before to you label, and display the X there.
CSS to display X after label
.input-invalid.input-focused label:after,
.input-invalid.input-has-value label:after {
content: " \2716";
}

Firefox has no "x" to remove content on input type="search" [duplicate]

Is there a quick way to create an input text element with an icon on the right to clear the input element itself (like the google search box)?
I looked around but I only found how to put an icon as background of the input element. Is there a jQuery plugin or something else?
I want the icon inside the input text element, something like:
--------------------------------------------------
| X|
--------------------------------------------------
Add a type="search" to your input
The support is pretty decent but will not work in IE<10
<input type="search">
Older browsers
If you need IE9 support here are some workarounds
Using a standard <input type="text"> and some HTML elements:
/**
* Clearable text inputs
*/
$(".clearable").each(function() {
const $inp = $(this).find("input:text"),
$cle = $(this).find(".clearable__clear");
$inp.on("input", function(){
$cle.toggle(!!this.value);
});
$cle.on("touchstart click", function(e) {
e.preventDefault();
$inp.val("").trigger("input");
});
});
/* Clearable text inputs */
.clearable{
position: relative;
display: inline-block;
}
.clearable input[type=text]{
padding-right: 24px;
width: 100%;
box-sizing: border-box;
}
.clearable__clear{
display: none;
position: absolute;
right:0; top:0;
padding: 0 8px;
font-style: normal;
font-size: 1.2em;
user-select: none;
cursor: pointer;
}
.clearable input::-ms-clear { /* Remove IE default X */
display: none;
}
<span class="clearable">
<input type="text" name="" value="" placeholder="">
<i class="clearable__clear">×</i>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Using only a <input class="clearable" type="text"> (No additional elements)
set a class="clearable" and play with it's background image:
/**
* Clearable text inputs
*/
function tog(v){return v ? "addClass" : "removeClass";}
$(document).on("input", ".clearable", function(){
$(this)[tog(this.value)]("x");
}).on("mousemove", ".x", function( e ){
$(this)[tog(this.offsetWidth-18 < e.clientX-this.getBoundingClientRect().left)]("onX");
}).on("touchstart click", ".onX", function( ev ){
ev.preventDefault();
$(this).removeClass("x onX").val("").change();
});
// $('.clearable').trigger("input");
// Uncomment the line above if you pre-fill values from LS or server
/*
Clearable text inputs
*/
.clearable{
background: #fff url(http://i.stack.imgur.com/mJotv.gif) no-repeat right -10px center;
border: 1px solid #999;
padding: 3px 18px 3px 4px; /* Use the same right padding (18) in jQ! */
border-radius: 3px;
transition: background 0.4s;
}
.clearable.x { background-position: right 5px center; } /* (jQ) Show icon */
.clearable.onX{ cursor: pointer; } /* (jQ) hover cursor style */
.clearable::-ms-clear {display: none; width:0; height:0;} /* Remove IE default X */
<input class="clearable" type="text" name="" value="" placeholder="" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
The trick is to set some right padding (I used 18px) to the input and push the background-image right, out of sight (I used right -10px center).
That 18px padding will prevent the text hide underneath the icon (while visible).
jQuery will add the class "x" (if input has value) showing the clear icon.
Now all we need is to target with jQ the inputs with class x and detect on mousemove if the mouse is inside that 18px "x" area; if inside, add the class onX.
Clicking the onX class removes all classes, resets the input value and hides the icon.
7x7px gif:
Base64 string:
data:image/gif;base64,R0lGODlhBwAHAIAAAP///5KSkiH5BAAAAAAALAAAAAAHAAcAAAIMTICmsGrIXnLxuDMLADs=
Could I suggest, if you're okay with this being limited to html 5 compliant browsers, simply using:
<input type="search" />
JS Fiddle demo
Admittedly, in Chromium (Ubuntu 11.04), this does require there to be text inside the input element before the clear-text image/functionality will appear.
Reference:
Dive Into HTML 5: A form of Madness.
input type=search - search field (NEW) HTML5.
According to MDN, <input type="search" /> is currently supported in all modern browsers:
<input type="search" value="Clear this." />
However, if you want different behavior that is consistent across browsers here are some light-weight alternatives that only require JavaScript:
Option 1 - Always display the 'x': (example here)
Array.prototype.forEach.call(document.querySelectorAll('.clearable-input>[data-clear-input]'), function(el) {
el.addEventListener('click', function(e) {
e.target.previousElementSibling.value = '';
});
});
.clearable-input {
position: relative;
display: inline-block;
}
.clearable-input > input {
padding-right: 1.4em;
}
.clearable-input > [data-clear-input] {
position: absolute;
top: 0;
right: 0;
font-weight: bold;
font-size: 1.4em;
padding: 0 0.2em;
line-height: 1em;
cursor: pointer;
}
.clearable-input > input::-ms-clear {
display: none;
}
<p>Always display the 'x':</p>
<div class="clearable-input">
<input type="text" />
<span data-clear-input>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>
Option 2 - Only display the 'x' when hovering over the field: (example here)
Array.prototype.forEach.call(document.querySelectorAll('.clearable-input>[data-clear-input]'), function(el) {
el.addEventListener('click', function(e) {
e.target.previousElementSibling.value = '';
});
});
.clearable-input {
position: relative;
display: inline-block;
}
.clearable-input > input {
padding-right: 1.4em;
}
.clearable-input:hover > [data-clear-input] {
display: block;
}
.clearable-input > [data-clear-input] {
display: none;
position: absolute;
top: 0;
right: 0;
font-weight: bold;
font-size: 1.4em;
padding: 0 0.2em;
line-height: 1em;
cursor: pointer;
}
.clearable-input > input::-ms-clear {
display: none;
}
<p>Only display the 'x' when hovering over the field:</p>
<div class="clearable-input">
<input type="text" />
<span data-clear-input>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>
Option 3 - Only display the 'x' if the input element has a value: (example here)
Array.prototype.forEach.call(document.querySelectorAll('.clearable-input'), function(el) {
var input = el.querySelector('input');
conditionallyHideClearIcon();
input.addEventListener('input', conditionallyHideClearIcon);
el.querySelector('[data-clear-input]').addEventListener('click', function(e) {
input.value = '';
conditionallyHideClearIcon();
});
function conditionallyHideClearIcon(e) {
var target = (e && e.target) || input;
target.nextElementSibling.style.display = target.value ? 'block' : 'none';
}
});
.clearable-input {
position: relative;
display: inline-block;
}
.clearable-input > input {
padding-right: 1.4em;
}
.clearable-input >[data-clear-input] {
display: none;
position: absolute;
top: 0;
right: 0;
font-weight: bold;
font-size: 1.4em;
padding: 0 0.2em;
line-height: 1em;
cursor: pointer;
}
.clearable-input > input::-ms-clear {
display: none;
}
<p>Only display the 'x' if the `input` element has a value:</p>
<div class="clearable-input">
<input type="text" />
<span data-clear-input>×</span>
</div>
<div class="clearable-input">
<input type="text" value="Clear this." />
<span data-clear-input>×</span>
</div>
You could use a reset button styled with an image...
<form action="" method="get">
<input type="text" name="search" required="required" placeholder="type here" />
<input type="reset" value="" alt="clear" />
</form>
<style>
input[type="text"]
{
height: 38px;
font-size: 15pt;
}
input[type="text"]:invalid + input[type="reset"]{
display: none;
}
input[type="reset"]
{
background-image: url( http://png-5.findicons.com/files/icons/1150/tango/32/edit_clear.png );
background-position: center center;
background-repeat: no-repeat;
height: 38px;
width: 38px;
border: none;
background-color: transparent;
cursor: pointer;
position: relative;
top: -9px;
left: -44px;
}
</style>
See it in action here: http://jsbin.com/uloli3/63
I've created a clearable textbox in just CSS. It requires no javascript code to make it work
below is the demo link
http://codepen.io/shidhincr/pen/ICLBD
Since none of the solutions flying around really met our requirements, we came up with a simple jQuery plugin called jQuery-ClearSearch -
using it is as easy as:
<input class="clearable" type="text" placeholder="search">
<script type="text/javascript">
$('.clearable').clearSearch();
</script>
​
Example: http://jsfiddle.net/wldaunfr/FERw3/
If you want it like Google, then you should know that the "X" isn't actually inside the <input> -- they're next to each other with the outer container styled to appear like the text box.
HTML:
<form>
<span class="x-input">
<input type="text" class="x-input-text" />
<input type="reset" />
</span>
</form>
CSS:
.x-input {
border: 1px solid #ccc;
}
.x-input input.x-input-text {
border: 0;
outline: 0;
}
Example: http://jsfiddle.net/VTvNX/
Change the text box type as 'search' in the design mode or
<input type="search">
EDIT: I found this link. Hope it helps. http://viralpatel.net/blogs/2011/02/clearable-textbox-jquery.html
You have mentioned you want it on the right of the input text. So, the best way would be to create an image next to the input box. If you are looking something inside the box, you can use background image but you may not be able to write a script to clear the box.
So, insert and image and write a JavaScript code to clear the textbox.
Use simple absolute positioning - it's not that hard.
jQuery:
$('span').click(function(){
$('input', $(this).parent()).val('');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
Vanilla JS:
var spans = document.getElementsByTagName("span");
function clickListener(e) {
e.target.parentElement.getElementsByTagName("input")[0].value = "";
}
for (let i = 0; i < spans.length; i++) {
spans[i].addEventListener("click", clickListener);
}
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
<div style="position:relative; width:min-content;">
<input>
<span style="position:absolute;right:10px">x</span>
</div>
jQuery Mobile now has this built in:
<input type="text" name="clear" id="clear-demo" value="" data-clear-btn="true">
Jquery Mobile API TextInput docs
Something like this??
Jsfiddle Demo
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.searchinput{
display:inline-block;vertical-align: bottom;
width:30%;padding: 5px;padding-right:27px;border:1px solid #ccc;
outline: none;
}
.clearspace{width: 20px;display: inline-block;margin-left:-25px;
}
.clear {
width: 20px;
transition: max-width 0.3s;overflow: hidden;float: right;
display: block;max-width: 0px;
}
.show {
cursor: pointer;width: 20px;max-width:20px;
}
form{white-space: nowrap;}
</style>
</head>
<body>
<form>
<input type="text" class="searchinput">
</form>
<script src="jquery-1.11.3.min.js" type="text/javascript"></script> <script>
$(document).ready(function() {
$("input.searchinput").after('<span class="clearspace"><i class="clear" title="clear">&cross;</i></span>');
$("input.searchinput").on('keyup input',function(){
if ($(this).val()) {$(".clear").addClass("show");} else {$(".clear").removeClass("show");}
});
$('.clear').click(function(){
$('input.searchinput').val('').focus();
$(".clear").removeClass("show");
});
});
</script>
</body>
</html>
<form action="" method="get">
<input type="text" name="search" required="required" placeholder="type here" />
<input type="reset" value="" alt="clear" />
</form>
<style>
input[type="text"]
{
height: 38px;
font-size: 15pt;
}
input[type="text"]:invalid + input[type="reset"]{
display: none;
}
input[type="reset"]
{
background-image: url( http://png-5.findicons.com/files/icons/1150/tango/32/edit_clear.png );
background-position: center center;
background-repeat: no-repeat;
height: 38px;
width: 38px;
border: none;
background-color: transparent;
cursor: pointer;
position: relative;
top: -9px;
left: -44px;
}
</style>
You can do with this commands (without Bootstrap).
Array.from(document.querySelectorAll('.search-field')).forEach(field => {
field.querySelector('span').addEventListener('click', e => {
field.querySelector('input').value = '';
});
});
:root {
--theme-color: teal;
}
.wrapper {
width: 80%;
margin: 0 auto;
}
div {
position: relative;
}
input {
background:none;
outline:none;
display: inline-block;
width: 100%;
margin: 8px 0;
padding: 13px 15px;
padding-right: 42.5px;
border: 1px solid var(--theme-color);
border-radius: 5px;
box-sizing: border-box;
}
span {
position: absolute;
top: 0;
right: 0;
margin: 8px 0;
padding: 13px 15px;
color: var(--theme-color);
font-weight: bold;
cursor: pointer;
}
span:after {
content: '\2716';
}
<div class="wrapper">
<div class="search-field">
<input placeholder="Search..." />
<span></span>
</div>
</div>
Here's a jQuery plugin (and a demo at the end).
http://jsfiddle.net/e4qhW/3/
I did it mostly to illustrate an example (and a personal challenge). Although upvotes are welcome, the other answers are well handed out on time and deserve their due recognition.
Still, in my opinion, it is over-engineered bloat (unless it makes part of a UI library).
I have written a simple component using jQuery and bootstrap.
Give it a try: https://github.com/mahpour/bootstrap-input-clear-button
Using a jquery plugin I have adapted it to my needs adding customized options and creating a new plugin. You can find it here:
https://github.com/david-dlc-cerezo/jquery-clearField
An example of a simple usage:
<script src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script src='http://code.jquery.com/ui/1.10.3/jquery-ui.js'></script>
<script src='src/jquery.clearField.js'></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="css/jquery.clearField.css">
<table>
<tr>
<td><input name="test1" id="test1" clas="test" type='text'></td>
<td>Empty</td>
</tr>
<tr>
<td><input name="test2" id="test2" clas="test" type='text' value='abc'></td>
<td>Not empty</td>
</tr>
</table>
<script>
$('.test').clearField();
</script>
Obtaining something like this:
No need to include CSS or image files. No need to include that whole heavy-artillery jQuery UI library. I wrote a lightweight jQuery plugin that does the magic for you. All you need is jQuery and the plugin. =)
Fiddle here: jQuery InputSearch demo.

Underlining Text in an <input> Box

I've gotten a request from a client to underline text in a text field.
Both single and double lines.
Is this even possible? I assume with some obscure plugin but I haven't found it yet. :P
I've thought about there being 2 possibilities, if this is possible.
Underlining the text in the actual field.
Doing some crazy hack with text just underneath the text field.
Thanks for any help and/or commentary. ^_^
The following works in Chrome 6.0.472.55/Ubuntu 10.04, and Firefox 3.6.9 (also Ubuntu 10.04):
input {text-decoration: underline; }
Though this, obviously, gives only a single-underline.
Quick demo at: jsbin
You could just use a CSS based solution i.e. a CSS selector to underline the text.
Here's a very basic example of how to accomplish this.
Still can't do it out of the box, but fairly simple to do with a little Sass and JS
Codepen example
<h1>Underline search input</h1>
<form class="search">
<label class="inputs">
<span class="label-tag">Search</span>
<input placeholder="Search" type="text" value="">
<span class="search-highlight"></span>
<span class="search-highlight-second"></span>
</label>
<button type="submit"> Search </button>
</form>
form {
label {
position: relative;
.label-tag {
display: none;
}
.search-highlight, .search-highlight-second {
user-select: none;
border-top: 1px solid #75d46b;
position: absolute;
left: 0;
bottom: -2px;
max-width: 100%;
height: 0;
color: transparent;
overflow: hidden;
}
.search-highlight-second {
bottom: -4px;
}
input {
border: none;
&:focus {
outline: none;
}
}
}
button {
display: block;
margin-top: 10px;
}
}
const input = document.querySelector('input');
const highlightEl = document.getElementsByClassName('search-highlight')
const highlightElTwo = document.getElementsByClassName('search-highlight-second')
input.oninput = handleInput;
function handleInput(e) {
console.log(e.target.value)
highlightEl[0].innerHTML = e.target.value
highlightElTwo[0].innerHTML = e.target.value
}