Get the focus and defocus method of a text box with jquery

Source: Internet
Author: User

I still like to use jquery to achieve, no matter how many input boxes on the page need to focus, lose focus, all public, my usual method is:

Traversing the input box on the page, get the Val value in the box, and when the input box is focused, compare it to the stored oldvalue value, and if the value is the same, change the value to empty, and when it loses focus, the original stored value is then assigned to Val, the code is as follows:

$ (function () {    $ ("input[class*=input]"). each (function () {         var oldvalue=$ (this). Val ();                $ (this). focus (function () {             if ($ (). Val () ==oldvalue) {            $ (this). Val (");            }           })       . Blur (function () {        if ($ (this). val () = = "") {            $ (this). Val (OldValue)})          })

JS Implementation method One: Directly in input to add
<input type= "text" onfocus= "if (this.value== ' Focus bar ') this.value="; "onblur=" if (this.value== ') this.value= ' focus '; " Value= "Spotlight" Name= "name" >

JS Implementation Method Two:

HTML code :

The JS code is as follows:

function AddListener (ELEMENT,E,FN) {         if (element.addeventlistener) {              element.addeventlistener (e,fn,false);         } else{             element.attachevent ("on" + E,FN);         }     }     var myinput = document.getElementById ("Myinput");     AddListener (Myinput, "click", Function () {         myinput.value = "";     })     AddListener (Myinput, "blur", function () {         Myinput.value = "Focus";     })

Of course, when the page is focused and out of focus, I recommend using jquery for this implementation.

Get the focus and defocus method of a text box with jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.