102

Having some trouble with bootstrap, so some help would be awesome. Thanks

What I would like:(top part)

enter image description here

My current code:

<div class="form-group">
    <input type="text" 
           class="form-control" 
           id="findJobTitle" 
           name="findJobTitle" 
           placeholder="Job Title, Keywords" 
           onkeyup="showResult()"> 
</div>

Current Screenshot:

enter image description here

zessx
  • 68,042
  • 28
  • 135
  • 158
K2SO Ghost Spirit
  • 1,291
  • 3
  • 11
  • 11

9 Answers9

214

To get something like this

input with clear button

with Bootstrap 3 and Jquery use the following HTML code:

<div class="btn-group">
  <input id="searchinput" type="search" class="form-control">
  <span id="searchclear" class="glyphicon glyphicon-remove-circle"></span>
</div>

and some CSS:

#searchinput {
    width: 200px;
}
#searchclear {
    position: absolute;
    right: 5px;
    top: 0;
    bottom: 0;
    height: 14px;
    margin: auto;
    font-size: 14px;
    cursor: pointer;
    color: #ccc;
}

and Javascript:

$("#searchclear").click(function(){
    $("#searchinput").val('');
});

Of course you have to write more Javascript for whatever functionality you need, e.g. to hide the 'x' if the input is empty, make Ajax requests and so on. See http://www.bootply.com/121508

unwired
  • 2,272
  • 1
  • 13
  • 5
  • 4
    If one prefers Plunker over Bootply, here it is: http://plnkr.co/edit/c2710u?p=preview – tanguy_k Mar 25 '14 at 16:02
  • 2
    From an accessibility point-of-view, it would be better for the `#searchclear` span to be a button. It's an interactive element, so mark it up as an interactive element. As it stands, users who rely on keyboard navigation can't tab to the searchclear span, and it won't easily be recognised by assistive technologies (such as screen readers) – Ian Dickinson Jan 26 '15 at 11:32
  • 3
    I just tried a fix for this in case you're using this input inline with other elements (like addons, dropdowns, etc) Just remove the class of the wrapping div, set it to `position: relative` and then adjust #searchclear with `z-index: 10; top: 10px` and remove `bottom: 0` – RecuencoJones Jul 08 '15 at 12:55
  • Isn't it a contradiction to have top, bottom, and height? – nafg Sep 22 '16 at 07:30
111

Super-simple HTML 5 Solution:

<input type="search" placeholder="Search..." />

Source: HTML 5 Tutorial - Input Type: Search

That works in at least Chrome 8, Edge 14, IE 10, and Safari 5 and does not require Bootstrap or any other library. (Unfortunately, it seems Firefox does not support the search clear button yet.)

After typing in the search box, an 'x' will appear which can be clicked to clear the text. This will still work as an ordinary edit box (without the 'x' button) in other browsers, such as Firefox, so Firefox users could instead select the text and press delete, or...

If you really need this nice-to-have feature supported in Firefox, then you could implement one of the other solutions posted here as a polyfill for input[type=search] elements. A polyfill is code that automatically adds a standard browser feature when the user's browser doesn't support the feature. Over time, the hope is that you'd be able to remove polyfills as browsers implement the respective features. And in any case, a polyfill implementation can help to keep the HTML code cleaner.

By the way, other HTML 5 input types (such as "date", "number", "email", etc.) will also degrade gracefully to a plain edit box. Some browsers might give you a fancy date picker, a spinner control with up/down buttons, or (on mobile phones) a special keyboard that includes the '@' sign and a '.com' button, but to my knowledge, all browsers will at least show a plain text box for any unrecognized input type.


Bootstrap 3 & HTML 5 Solution

Bootstrap 3 resets a lot of CSS and breaks the HTML 5 search cancel button. After the Bootstrap 3 CSS has been loaded, you can restore the search cancel button with the CSS used in the following example:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<style>
  input[type="search"]::-webkit-search-cancel-button {
    -webkit-appearance: searchfield-cancel-button;
  }
</style>
<div class="form-inline">
  <input type="search" placeholder="Search..." class="form-control" />
</div>

Source: HTML 5 Search Input Does Not Work with Bootstrap

I have tested that this solution works in the latest versions of Chrome, Edge, and Internet Explorer. I am not able to test in Safari. Unfortunately, the 'x' button to clear the search does not appear in Firefox, but as above, a polyfill could be implemented for browsers that don't support this feature natively (i.e. Firefox).

YipYip
  • 1,375
  • 1
  • 9
  • 8
  • 2
    FWIW: I don't believe this works in Chrome anymore. Maybe not a part of the official CSS spec anymore? W3Schools says "a search field behaves like a regular text field" and MDN makes no mention of this functionality. MDN does say it will strip new lines but that's about it for this input type. – KyleFarris May 16 '17 at 20:53
  • 3
    @KyleFarris Both methods above are still working for me in Chrome version 58.0.3029.110 (64-bit). You need to type something into the search box before the clear button will appear. – YipYip May 18 '17 at 01:01
  • How does this super-rated answer address the OP issue to display a "clear" button inside the text box that clears its content? – usr-local-ΕΨΗΕΛΩΝ Oct 26 '18 at 09:00
  • @usr-local-ΕΨΗΕΛΩΝ That's exactly what you get with the answer I provided, in most browsers. Please note that you do have to type something before the 'x' clear button will appear! And, as stated in the answer, Firefox does not support this feature natively. I addressed that with a suggestion of implementing one of the other solutions as a polyfill. I have edited my answer slightly to hopefully make it more clear (no pun intended). – YipYip Oct 30 '18 at 04:17
  • 3
    It's not a super-simple solution if it doesn't work in all major browsers. – thelem Nov 09 '18 at 11:59
  • 1
    @AnandUndavia Thanks for reporting that. I have updated my answer to specify "Bootstrap 3", which is what the OP was using when the question was posted (back in 2013). I will look into making my answer work for Bootstrap 4 when I have the time. – YipYip Jul 07 '19 at 03:55
  • Thanks a lot for your solution for bootstrap 3 , it seems 'searchfield-cancel-button' not work for Safari – Buffer Sep 25 '20 at 07:16
23

I tried to avoid too much custom CSS and after reading some other examples I merged the ideas there and got this solution:

<div class="form-group has-feedback has-clear">
    <input type="text" class="form-control" ng-model="ctrl.searchService.searchTerm" ng-change="ctrl.search()" placeholder="Suche"/>
    <a class="glyphicon glyphicon-remove-sign form-control-feedback form-control-clear" ng-click="ctrl.clearSearch()" style="pointer-events: auto; text-decoration: none;cursor: pointer;"></a>
</div>

As I don't use bootstrap's JavaScript, just the CSS together with Angular, I don't need the classes has-clear and form-control-clear, and I implemented the clear function in my AngularJS controller. With bootstrap's JavaScript this might be possible without own JavaScript.

Paul Wellner Bou
  • 532
  • 5
  • 16
18

Thanks unwired your solution was very clean. I was using horizontal bootstrap forms and made a couple modifications to allow for a single handler and form css.

html: - UPDATED to use Bootstrap's has-feedback and form-control-feedback

 <div class="container">
  <form class="form-horizontal">
   <div class="form-group has-feedback">
    <label for="txt1" class="col-sm-2 control-label">Label 1</label>
    <div class="col-sm-10">
     <input id="txt1" type="text" class="form-control hasclear" placeholder="Textbox 1">
     <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
    </div>
   </div>
   <div class="form-group has-feedback">
    <label for="txt2" class="col-sm-2 control-label">Label 2</label>
    <div class="col-sm-10">
      <input id="txt2" type="text" class="form-control hasclear" placeholder="Textbox 2">
      <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
    </div>
   </div>
   <div class="form-group has-feedback">
    <label for="txt3" class="col-sm-2 control-label">Label 3</label>
    <div class="col-sm-10">
     <input id="txt3" type="text" class="form-control hasclear" placeholder="Textbox 3">
     <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
    </div>   
   </div>
  </form>
 </div>

javascript:

$(".hasclear").keyup(function () {
    var t = $(this);
    t.next('span').toggle(Boolean(t.val()));
});
$(".clearer").hide($(this).prev('input').val());
$(".clearer").click(function () {
    $(this).prev('input').val('').focus();
    $(this).hide();
});

example: http://www.bootply.com/130682

LDAdams
  • 682
  • 4
  • 18
  • 8
    After bootstrap v3.3.0 you will need to set the pointer-events from none to auto otherwise you will not be able to click on the form-control-feedback. – Vizjerai Mar 18 '15 at 13:48
  • What does `$(".clearer").hide($(this).prev('input').val());` do? Can't it just be `$(".clearer").hide();`? – Jim Buck Apr 29 '15 at 20:51
  • I assume his intent was to pass a truthy paramter to hide() to start off hidden/shown based on if the input has a value already. If that was the intent, he failed (hide always hides). It should be `$(".clearer").toggle(!!$(this).prev('input').val());` – Jeff Shepler Jun 10 '15 at 15:33
15

AngularJS / UI-Bootstrap Answer

  • Use Bootstrap's has-feedback class to show an icon inside the input field.
  • Make sure the icon has style="cursor: pointer; pointer-events: all;"
  • Use ng-click to clear the text.

JavaScript (app.js)

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope) {

  $scope.params = {};

  $scope.clearText = function() {
    $scope.params.text = null;
  }

});

HTML (index.html snippet)

      <div class="form-group has-feedback">
        <label>text box</label>
        <input type="text"
               ng-model="params.text"
               class="form-control"
               placeholder="type something here...">
        <span ng-if="params.text"
              ng-click="clearText()"
              class="glyphicon glyphicon-remove form-control-feedback" 
              style="cursor: pointer; pointer-events: all;"
              uib-tooltip="clear">
        </span>
      </div>

Here's the plunker: http://plnkr.co/edit/av9VFw?p=preview

Derek Soike
  • 11,238
  • 3
  • 79
  • 74
1

Do it with inline styles and script:

<div class="btn-group has-feedback has-clear">
    <input id="searchinput" type="search" class="form-control" style="width:200px;">
        <a 
id="searchclear" 
class="glyphicon glyphicon-remove-circle form-control-feedback form-control-clear" 
style="pointer-events:auto; text-decoration:none; cursor:pointer;"
onclick="$(this).prev('input').val('');return false;">
</a>
</div>
1

Here is my working solution with search and clear icon for Angularjs\Bootstrap with CSS.

    <div class="form-group has-feedback has-clear">
                    <input type="search" class="form-control removeXicon" ng-model="filter" style="max-width:100%" placeholder="Search..." />
                    <div ng-switch on="!!filter">
                        <div ng-switch-when="false">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                        <div ng-switch-when="true">
                            <span class="glyphicon glyphicon-remove-sign form-control-feedback" ng-click="$parent.filter = ''" style="pointer-events: auto; text-decoration: none;cursor: pointer;"></span>
                        </div>
                    </div>                        
                </div>

------

CSS

/* hide the built-in IE10+ clear field icon */
.removeXicon::-ms-clear {
  display: none;
}

/* hide the built-in chrome clear field icon */
.removeXicon::-webkit-search-decoration,
.removeXicon::-webkit-search-cancel-button,
.removeXicon::-webkit-search-results-button,
.removeXicon::-webkit-search-results-decoration { 
      display: none; 
}
PavanT
  • 81
  • 8
1

Do not bind to element id, just use the 'previous' input element to clear.

CSS:

.clear-input > span {
    position: absolute;
    right: 24px;
    top: 10px;
    height: 14px;
    margin: auto;
    font-size: 14px;
    cursor: pointer;
    color: #AAA;
}

Javascript:

function $(document).ready(function() {
    $(".clear-input>span").click(function(){
        // Clear the input field before this clear button
        // and give it focus.

        $(this).prev().val('').focus();
    });
});

HTML Markup, use as much as you like:

<div class="clear-input">
    Pizza: <input type="text" class="form-control">
    <span class="glyphicon glyphicon-remove-circle"></span>
</div>

<div class="clear-input">
    Pasta: <input type="text" class="form-control">
    <span class="glyphicon glyphicon-remove-circle"></span>
</div>
pizzamonster
  • 1,141
  • 10
  • 9
-7

Place the image (cancel icon) with position absolute, adjust top and left properties and call method onclick event which clears the input field.

<div class="form-control">
    <input type="text" id="inputField" />
</div>
<span id="iconSpan"><img src="icon.png" onclick="clearInputField()"/></span>

In css position the span accordingly,

#iconSpan {
 position : absolute;
 top:1%;
 left :14%;
}
Brandon
  • 68,708
  • 30
  • 194
  • 223
code2use
  • 56
  • 3