0

I made a search bar with search icon. I want this, when I click the #forminput, #search will return white; and after I click another object, #search will return lightgray color. How can I do this? My codes:

$(document).ready(function(){
$("#forminput").click(function(){
$("#search").css("color", "white");
});
});

Have a nice day.

2 Answers2

0

Yes, you can use off and give the event handler:

$("#forminput").off("click");

But if you want to restore to an earlier state, it is always wise to use just CSS for presentation and then use JavaScript to toggle between the classes. Considering your current situation, I believe just CSS is enough.

input {color: #999; padding: 10px; width: 200px;}
input:focus {color: #000;}
<input type="text" value="Default Gray, Click Black." />
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Here you go with the solution https://jsfiddle.net/8gygvdwy/

$('input[type=text]').focus(function(){
 $(this).css({
   'background': '#ccc'
  });
}).focusout(function(){
 $(this).css({
   'background': '#fff'
  });
});

$('input[type=button]').click( function(){
 $('input[type=text]').css({
   'background': '#fff'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" />
<input type="button" value="Submit" />
Shiladitya
  • 12,003
  • 15
  • 25
  • 38