开发者

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 defau开发者_Go百科lt value changes color, and the text i'll type, another one.

How can i do that?


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


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.


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'; }"> 


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");
         }
      );
   }
)



// '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;
    }
})();


Here we go:

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

Best of luck!

Keep coding!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜