Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide input value on focus?

I see this all over the web, but was wondering if anyone has the JavaScript code for the EASIEST way to show input value on blur, but hide in on focus.

like image 764
Michal Kopanski Avatar asked Jul 03 '09 05:07

Michal Kopanski


People also ask

How do you set the input element to focus?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do I remove input from focus form?

Use the blur() method to remove the focus from an element, e.g. input. blur() . If you need to remove the focus from the currently active element without selecting it, call the blur method on the activeElement property - document.

How do you know if input is in focus?

To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.


1 Answers

This always worked for me:

<input 
    type="text" 
    value="Name:"
    name="visitors_name" 
    onblur="if(value=='') value = 'Name:'" 
    onfocus="if(value=='Name:') value = ''"
 />
like image 109
JLGarcia Avatar answered Oct 06 '22 00:10

JLGarcia