nanobash
nanobash

Reputation: 5500

ignore text from input onfocus

I've a snag , here it is simple case

<input type="text" id="some" value="something">

and javascript

document.getElementById("some").onfocus = function(){
    this.style.opacity = 0.5;
};

It works just fine , but I want also that during this time (onfocus) mouse pointer be at the beginning of input field ( I mean to ignore default value , not to continue from "something") , any ideas ? thanks :))

P.S This is the just same like it is on facebook search, when focus search field and the default text become dim and the pointer moves at the beginning of input field

Upvotes: 0

Views: 234

Answers (2)

JSW189
JSW189

Reputation: 6325

You could do something like this using the onkeypress event. Granted not the greatest piece of code, but it should get you started.

HTML:

<input type="text" id="some" value="something">

​ Javascript:

i = 1;
document.getElementById("some").onkeypress= function(){    
    if(i === 1) {
        document.getElementById("some").value = '';
        i = 0;
    }
    this.style.color = 'black';
}

document.getElementById("some").onfocus = function(){
    this.style.color = 'gray';
};

​ JS Fiddle: http://jsfiddle.net/wZAvn/

Upvotes: 1

Mario Michelli
Mario Michelli

Reputation: 543

or something like this:

var some = document.getElementById("some"),
val = some.value; 
some.onfocus = function(){
    some.style.opacity = 0.5;
    some.value = (some.value == val )?"":some.value;        
};​

http://jsfiddle.net/w5Kv8/1/

Upvotes: 0

Related Questions