label inside input text box - html

I'm trying to implement a mockup given below, I looked around to find something similar but no luck. Could some one guide me how to do this.
So problem here is need to show static "from" "to" and "90 days" text every time.
Note "From","To","days" and Icon should be inside text box

This is a VERY simple example intended to get you started.
The key point is: use jQuery UI's "datepicker" module. It is powerful - you can auto-calculate the 90 day calc and, upon leaving the Date From field, have it auto-calc 90 days and plunk that date into the From field as a default.
Look how much you get done with how little.
$( ".dp" ).datepicker();
/* */
div{position:relative;box-sizing:border-box;}
.row{overflow:hidden;border:2px solid #aaa;padding:5px;}
.r-left {float:left;width:33%;margin-right:10px;}
.r-right{float:left;width:33%;margin:0 10px;}
.farright{display:flex;height:50px;}
.inlineblock{width:60px;height:50px;margin:auto;flex-flow: row nowrap;justify-content:flex-end;align-content:center;}
<link href="http://code.jquery.com/ui/1.9.2/themes/cupertino/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script
src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"
integrity="sha256-eEa1kEtgK9ZL6h60VXwDsJ2rxYCwfxi40VZ9E0XwoEA="
crossorigin="anonymous"></script>
<div class="row">
<div class="r-left">
<div>FROM</div>
<div><input id="datFrom" class="dp" type="text" /></div>
</div>
<div class="r-right">
<div>TO</div>
<div><input id="datTo" class="dp" type="text" /></div>
</div>
<div class="farright">
<div class="inlineblock">
<br>90 days
</div>
<div class="inlineblock" id="90days">
<img src="http://placeimg.com/50/50/nature" />
</div>
</div>
</div>

I have tweak the code from gibberish and see if this serve your purpose.
and for the label inside textbox set placeholder attribute of input tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script
src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"
integrity="sha256-eEa1kEtgK9ZL6h60VXwDsJ2rxYCwfxi40VZ9E0XwoEA="
crossorigin="anonymous"></script>
<script>
$(function() {
$( ".dp" ).datepicker();
});
</script>
<style>
div{position:relative;box-sizing:border-box;}
.row{overflow:hidden;border:2px solid #aaa;padding:5px;}
.r-left {float:left;width:30%;margin-right:10px;}
.r-right{float:left;width:30%;margin:0 10px;}
.farright{float:right;width:30%;margin-left:10px;}
.inlineblock{display:inline-block;color: grey;}
#90days{margin-left:15px;margin-top:-35px;color: red;}
#datFrom{
border: 0px;
}
#datTo{
border: 0px;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 15px;
top: 20px;
transition: 0.2s ease all;
font-size: 20px;
}
.floating-label1 {
position: absolute;
pointer-events: none;
left: 215px;
top: 20px;
transition: 0.2s ease all;
font-size: 20px;
}
.inputText {
font-size: 14px;
width: 185px;
height: 35px;
}
input:focus ~ .floating-label,input:not(:focus):valid ~ .floating-label{
top: 15px;
bottom: 10px;
left: 15px;
font-size: 11px;
opacity: 1;
color: grey;
}
input:focus ~ .floating-label1,input:not(:focus):valid ~ .floating-label1{
top: 15px;
bottom: 10px;
left: 215px;
font-size: 11px;
opacity: 1;
color: grey;
}
.calender-css{
height:35px;
vertical-align: middle;
}
input:focus {
border-color:white;
}
</style>
<link href="http://code.jquery.com/ui/1.9.2/themes/cupertino/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script
src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"
integrity="sha256-eEa1kEtgK9ZL6h60VXwDsJ2rxYCwfxi40VZ9E0XwoEA="
crossorigin="anonymous"></script>
<div class="row">
<div class="r-left">
<!--<div>FROM</div>-->
<div><input id="datFrom" class="dp inputText" type="text" required/><span class="floating-label">From</span></div>
</div>
<div class="r-right">
<!--<div>TO</div>-->
<div><input id="datTo" class="dp inputText" type="text" required/><span class="floating-label1">TO</span></div>
</div>
<div class="farright">
<div class="inlineblock" id="90days">
90 days
</div>
<div class="inlineblock">
<img src="http://www.freeiconspng.com/uploads/calendar-icon-png-14.png" class="calender-css" />
</div>
</div>
</div>

Related

CSS Sibling Selector not animating [duplicate]

This question already has answers here:
Why doesn't the adjacent sibling selector work? [closed]
(5 answers)
Closed 5 years ago.
I'm trying to animate a <p> tag on its sibling <input>'s focus but it won't work.
I've made a codepen bellow.
I also don't want to use Javascript for the solution.
https://codepen.io/ItsBrianAgar/pen/YrGVxW?editors=0100
HTML
<html lang="en"></html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700,900" rel="stylesheet"/>
<link href="assets/css/main.css" type="text/css" rel="stylesheet"/>
<title>myCoffee</title>
</head>
<body>
<main class="appContainer">
<div class="app">
<div class="sectionLogin">
<div class="logo"><img src="assets/images/logo.svg" alt="logo"/></div>
<div class="inputLogin">
<div class="textField"></div>
<div class="textField">
<p id="username">Username</p>
<input type="text" name="username"/>
</div>
<div class="textField">
<p id="password">Password</p>
<input type="password" name="password"/>
</div>
</div>
<div class="btnLogin">
<button>LOGIN</button>
</div>
<div class="btnSignup">
<button>SIGN UP</button>
</div>
</div>
</div>
</main>
</body>
CSS
input[name="username"]:focus ~ #username {
perspective: 1000;
transform: translate3d(-50%, 30px, 0);
opacity: 0;
transition: all 150ms ease-out; }
input[name="password"]:focus ~ #password {
perspective: 1000;
transform: translate3d(-50%, 30px, 0);
opacity: 0;
transition: all 150ms ease-out; }
It doesn't work because ~ selector works in other way. It selects all the siblings that comes after main selector. In your case <p> comes before <input>.
To make it working you should make a little replacing in your html:
<div class="textField">
<input type="text" name="username"/>
<p id="username">Username</p>
</div>
I know that it may break the layout, but you're able to use position: absolute; to fix it.
To use + / ~ the p element must next to the input.
Here is the working demo:
.box {
padding: 30px;
}
.textField {
position: relative;
}
input {
padding: 10px;
width: 200px;
border-color: transparent transparent red transparent;
outline: none;
}
p{
margin: 0;
position: absolute;
bottom: 3px;
transition: all linear 0.25s;
}
input:focus + P {
font-size: 12px;
bottom: 30px;
}
<div class="box">
<div class="textField">
<input type="text" name="password"/>
<p id="password">Password</p>
</div>
</div>

Filter <div> with <input type="checkbox"> expandables and <label>,<p> text content

I have been trying to locate a way to filter out my expandable div's by the text content located within the div> label> and p>. I will place my example below. I have tried a few jquery examples from previous questions I found answered, but I cannot seem to make it work. The way I have the div's built, is to expand the p> content by clicking on the applicable label>. I would like to have a search box that would filter out div's that do not contain the text listed in the search box.
<html>
<head>
</head>
<body>
<style>
div {
position: relative;
}
input[type=checkbox] {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
h3 {
font-size: large;
font-weight: 400;
color: #4a19ff;
margin: 20px 0 10px 0;
}
label {
cursor: pointer;
position: relative;
display: block;
padding-left: 20px;
font-family: 'Helvetica', 'Arial';
color: gray;
}
label:before {
display: none;
content: "";
position: absolute;
width: 0;
height: 0;
top: 40%;
left: 10px;
border-left: 8px solid black;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
margin-top: -8px;
}
input[type=checkbox]:checked ~ h2 label:before {
border-left: 8px solid transparent;
border-top: 8px solid black;
border-right: 8px solid transparent;
margin-left: -4px;
margin-top: -4px
}
input[type=checkbox]:checked ~ h3 ~ p {
max-height: 80%;
color: white;
}
</style>
<center>
<input type="text" id="example" placeholder="Search example..">
</center>
<div class="code">
<input type="checkbox" id="1">
<h3>
<label for="1">1 Expandables</label>
</h3>
<p class="content">
This is an example of the expandables.
</p>
</input>
</div>
<div class="code">
<input type="checkbox" id="2">
<h3>
<label for="2">2 Test</label>
</h3>
<p class="content">
Can I do this?
</p>
</input>
</div>
<div class="code">
<input type="checkbox" id="3">
<h3>
<label for="3">3 Apology</label>
</h3>
<p class="content">
I am a noob at this and apologize if this is a basic request.
</p>
</input>
</div>
</body>
</html>
This script will get the value from textfield with id and name = "search", and then will iterate through all the div elements in the document body, and if any of these div elements doesn't contain a string entered into a searchbox, it will set it's display style to "none".
If you want to remove the div from the document completely you can change the this.style.display="none" line to
document.body.removeChild(this).
<html><head>
<title>Title</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
</head>
<body>
<input type="text" name="search" id="search" value="">
<button type="button" name="btnSearch" onclick="btnSearchOnClick()">Search</button>
<script type="text/javascript">
function btnSearchOnClick(){
var searchTxt = document.getElementById("search").value; // get value from search input field
if(searchTxt != ""){ // if there is input in search field
$("div").each(function(){ // iterate through each div on page
var index = this.innerHTML.indexOf(searchTxt);
if(index == -1){
this.style.display = "none";
}
});
}
}
</script><div id="div0" style="display: none;">div0</div>
<div id="div1" style="display: none;">div1</div>
<div id="div2" style="display: none;">div2</div>
<div id="div3" style="display: none;">div3</div>
<div id="div4" style="display: none;">div4</div>
<div id="div5" style="display: none;">div5</div>
<div id="div6" style="display: none;">div6</div>
<div id="div7" style="display: none;">div7</div>
<div id="div8" style="display: none;">div8</div>
<div id="div9" style="display: none;">div9</div>
</body></html>
EDIT:
Here is the new code that you asked for.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
</head>
<body>
<input type="text" name="searchbox" id="searchbox" />
<div id="div0">div0</div>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
<div id="div4">div4</div>
<div id="div5">div5</div>
<div id="div6">div6</div>
<div id="div7">div7</div>
<div id="div8">div8</div>
<div id="div9">div9</div>
<script type="text/javascript">
$("#searchbox").on("input", function(){
var searchboxText = $("#searchbox").val();
if(searchboxText != ""){
$("div").each(function(){
var divText = $(this).text();
var index = divText.indexOf(searchboxText);
if(index == -1){
$(this).css("display", "none");
}
else{
$(this).css("display", "block");
}
});
}
else{
$("div").each(function(){
$(this).css("display", "block");
});
}
});
</script>
</body>
</html>

Owl Carousel 2 pagination dots were not diplayed

I am experiencing issue. The navigation components owl-nav and owl-dots are no longer wrapped with owl-controls.
<div class="owl-nav disabled">
<div class="owl-prev">prev</div>
<div class="owl-next">next</div>
</div>
<div class="owl-dots disabled">
<div class="owl-dot active"><span></span></div>
<div class="owl-dot"><span></span></div>
</div>
Demos show:
<div class="owl-controls">
<div class="owl-nav disabled">
<div class="owl-prev">prev</div>
<div class="owl-next">next</div>
</div>
<div class="owl-dots disabled">
<div class="owl-dot active"><span></span></div>
<div class="owl-dot"><span></span></div>
</div>
</div>
This is breaking the default css selections within owl-theme.
Can any one help me to solve this issue?
Thanks in advance.
Had the same problem, used some CSS-Styling my own:
.owl-dots {
text-align: center;
position: fixed;
bottom: 5px;
width: 100%;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
backface-visibility: hidden;
}
.owl-dot {
border-radius: 50px;
height: 10px;
width: 10px;
display: inline-block;
background: rgba(127,127,127, 0.5);
margin-left: 5px;
margin-right: 5px;
}
.owl-dot.active {
background: rgba(127,127,127, 1);
}
If you don't want to overlaps the dots just use:
.owl-dots {
text-align: center;
}
You've to add owl-theme file also to get pagination dots.
$(document).ready(function(){
$('#demoCarousel').owlCarousel({
loop:true,
margin:10,
nav:true,
dots: true // if you don't want dots, change to false
});
});
#demoCarousel .item {
height: 10rem;
background: #4DC7A0;
padding: 1rem;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" type="text/css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<div class="owl-carousel owl-theme" id="demoCarousel">
<div class="item"><h4>1</h4></div>
<div class="item"><h4>2</h4></div>
<div class="item"><h4>3</h4></div>
<div class="item"><h4>4</h4></div>
<div class="item"><h4>5</h4></div>
<div class="item"><h4>6</h4></div>
<div class="item"><h4>7</h4></div>
<div class="item"><h4>8</h4></div>
<div class="item"><h4>9</h4></div>
<div class="item"><h4>10</h4></div>
<div class="item"><h4>11</h4></div>
<div class="item"><h4>12</h4></div>
</div>
I had same issue, Add more slides and dots will appear.

How to add a backdrop over a div (lightbox effect)?

i'm editing an AngularJS project and i want to add a backdrop (like Bootstrap Modal) over the main DIV when users click on search input box.
Here is the HTML code:
<div class="main-box">
<div class="search-function" ng-click="showInputForm()">
<img src="../images/my_project/search.png">
</div>
<div class="search-form" ng-if="showForm">
<form ng-submit="textSearch()">
<input type="text" autofocus class="search-input" ng-model="text.value" />
</form>
</div>
</div>
As you can see the white box appears when users click on the search icon and now i want to add a backdrop over the main DIV, except on the white box.
Here is the LESS code:
.search-function {
margin-left: 30%;
}
.search-form {
padding-left: 15%;
padding-right: 15%;
position: relative;
z-index: 0;
.search-input {
color: #2b84a6 !important;
background-color: #fff !important;
font-weight: 300;
font-size: 16px;
}
}
How can i reach this?
Something like this
<div class="main-box">
<div class="backdrop" ng-if="showForm"></div>
<div class="search-function" ng-click="showInputForm()">
<img src="../images/my_project/search.png">
</div>
<div class="search-form" ng-if="showForm">
<form ng-submit="textSearch()">
<input type="text" autofocus class="search-input" ng-model="text.value" />
</form>
</div>
</div>
And add the CSS as
.backdrop {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background: black;
opacity: 0.5;
}
Here's a very complete article about different methods of achieving what you need
CSS Overlay Techniques

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.