Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JSBin Demo by Roko C.B.</title>
</head>
<body>
  
  <h1>Clear icon inside input field</h1>
<input class="clearable" type="text" name="" value="" placeholder="Enter a Search term" />
  
  
  
</body>
</html>
 
body{text-align:center;}
.clearable{
  background:url(http://i.imgur.com/z7ZSYjt.png) no-repeat right -10px center;
  border:1px solid #999;
  padding:3px 18px 3px 4px; /* USE the same right padding in jQ! */
  border-radius:3px;
  transition: background 0.4s; /*REMOVE THIS LINE IF YOU ENCOUNTER ISSUES IN Chrome (Bug 02.2014)*/
}
.clearable.x{
  background-position: right 5px center;
}
.clearable.onX{
  cursor:pointer;
}
 
jQuery(function($) {
 
  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('click', '.onX', function(){
    $(this).removeClass('x onX').val('');
  });
  
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers