Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit HTML input value colour?

Tags:

html

forms

In my input field (text), I have a text which disappears once it is clicked. How can I make this text a shade lighter by editing its colour?

Sorry, the code looks messy, I had to chop it up to show you.

Thanks!

James

<form>
   <input type="text" name="search" size="35"    
    onclick="this.value='';"onfocus="this.select()"
    onblur="this.value=!this.value?'Job Title   
    e.g. Assistant Manager':this.value;"
    value="Job Title e.g. Assistant Manager" 
    style="background-color:white; border: 
    solid 1px #6E6E6E; height: 30px; font-size:18px; 
    vertical-align:9px"/>

   <input type="text" name="searchterm" size="35" 
    style="background-color:white; border: solid 1px #6E6E6E;
    height: 30px; font-size:18px; vertical-align:9px"/>

   <input type="image" src="but.tiff" alt="Submit" width="60">
</form>
like image 276
James Avatar asked Aug 25 '11 16:08

James


3 Answers

Maybe something like this (just add your style):

<input type="text" 
       size="35" 
       value="Job Title e.g. Assistant Manager" 
       style="background-color:white; 
              border: solid 1px #6E6E6E;
              height: 30px; 
              font-size:18px; 
              vertical-align:9px;color:#bbb" 
        onfocus="if(this.value == 'Job Title e.g. Assistant Manager') {
                    this.value = '';
                    this.style.color='#000';
                 }" />

<input type="text" 
       name="searchterm" size="35" 
       style="background-color:white; 
              border: solid 1px #6E6E6E;
              height: 30px; 
              font-size:18px; 
              vertical-align:9px" />

UPDATE: Since placeholder attribute is very well supported on all major browsers, there is no need to do anything manually. Its possible to achieve the same thing with this:

<input type="text" 
       size="35" 
       placeholder="Job Title e.g. Assistant Manager" />
like image 182
Bojan Dević Avatar answered Oct 10 '22 12:10

Bojan Dević


You can add color in the style rule of your input: color:#ccc;

like image 34
Kelly Cook Avatar answered Oct 10 '22 13:10

Kelly Cook


You can change the CSS color property using JavaScript in the onclick event handler (in the same way you change the value property):

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

Note that it's not the best practice to use inline JavaScript. You'd be better off giving your input an ID, and moving your JavaScript out to a <script> block instead:

document.getElementById("yourInput").onclick = function() {
    this.value = '';
    this.style.color = '#000';
}
like image 4
James Allardice Avatar answered Oct 10 '22 14:10

James Allardice