Simon
Simon

Reputation: 23141

Change text color in text field

I have an input text field, which has a value "something" by default, but when I start to type, I want that the default value changes color, and the text i'll type, another one.

How can i do that?


<input type="text" value="something" onclick="this.value=''" />

Upvotes: 6

Views: 30995

Answers (5)

Prince Issar
Prince Issar

Reputation: 1

// 'run' is an id for button and 'color' is for input tag

// code starts here
(function () {
    
    const button = document.getElementById("run");
    button.addEventListener("click", colorChange);

    function colorChange() {
        document.body.style.backgroundColor = document.getElementById("color").value;
    }
})();

Upvotes: 0

Otroberts
Otroberts

Reputation: 347

Here we go:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" />

Best of luck!

Keep coding!

Upvotes: -3

Levi Hackwith
Levi Hackwith

Reputation: 9332

Going off @chibu's answer, this is how you would do it using jQuery and unobtrusive Javascript


 
$(document).ready(
   function() {
      $("#mytext").bind(
         "click",
         function() {
            $(this).val("");
            $(this).css("color", "red");
         }
      );
   }
)


Upvotes: 0

Chibu
Chibu

Reputation: 1353

To keep it simple like your example:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" />

And that should pretty much do it.

Upvotes: 14

Daniel Vassallo
Daniel Vassallo

Reputation: 344271

You may want to try the following:

<input type="text" value="something"
       onFocus="if (this.value == 'something') this.style.color = '#ccc';"
       onKeyDown="if (this.value == 'something') {  
                      this.value = ''; this.style.color = '#000'; }"> 

Upvotes: 6

Related Questions