Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5982063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:02:00+00:00 2026-05-22T22:02:00+00:00

Is there a quick way to create an input text element with an icon

  • 0

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|
--------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:02:00+00:00Added an answer on May 22, 2026 at 10:02 pm

    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">&times;</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)

    Clear icon inside input element

    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(https://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: Clear icon 7x7

    Base64 string:

    data:image/gif;base64,R0lGODlhBwAHAIAAAP///5KSkiH5BAAAAAAALAAAAAAHAAcAAAIMTICmsGrIXnLxuDMLADs=
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a quick way to set an HTML text input ( <input type=text
Is there a quick way to join paths like the Join-Path function in Powershell?
is there a quick way to sort the items of a select element? Or
Is there any quick way to, given an executable file, create a Windows service
Is there a quick way to find every match of a regular expression in
Is there a quick way to detect classes in my application that are never
Is there a quick way to determine whether you are using certain namespaces in
In Spring 2.0, is there a quick way to have a list of the
I have an ASP.NET RegularExpressionValidator that checks file extensions. Is there a quick way
I'm just wondering if there is a quick way to echo undefined variables without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.