Mayur Sutariya
Mayur Sutariya

Reputation: 129

change word text different color when typing

I type in a text box which gives matched data then change the color of text and type second time then change other color then type again then again change color.

Upvotes: 4

Views: 779

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Bind input event handler to the textfield using on() and inside event handler change color from an array

var color = ['red', 'yellow', 'red', 'blue', 'brown'];
var i = 0;
$('#text').on('input',function() {
  $(this).css('color', color[i]);
  i = (i + 1) % color.length;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="text"/>


Or generate random color code using Math.random()

var color = ['red', 'yellow', 'red', 'blue', 'brown'];
$('#text').on('input', function() {
  $(this).css('color', "#" + (Math.random() * 16777215 | 0).toString(16));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="text" />

Upvotes: 3

Related Questions