jquery search Box Add default text implementation code

Source: Internet
Author: User

example, using JS lost focus and gain focus method

JS Code

The code is as follows Copy Code

<input type= "text" name= "s" value= "hint text" onfocus= "if (value = = ' Hint text ') {value = '} ' onblur= ' if (value = = ') {value= ' Prompt text '} '/>

How to implement in WordPress

The code is as follows Copy Code

<input type= "text" name= "s" id= "S" size= "15″value=" <?php Echo (Get_search_query)

? Get_search_query (): ' Your Default Text ');?> "onfocus=" if (this.value = ' Your

Default Text ') {this.value = ';} ' onblur= ' if (this.value = ") {this.value = ' <?php

_e (' Your Default Text ', ' Jessica ')?> ';} ' Name= "S" id= "s"/>

jquery Code

The code is as follows Copy Code
$ (document). Ready (function () {
Search box
$ (' #keyword '). focus (function () {
if ($ (this). val () = ' Please enter keywords! ') {
$ (this). Val ("");
}
});
$ (' #keyword '). blur (function () {
if ($ (this). val () = "") {
$ (this). Val (' Please enter keywords! ');
}
});
});


An instance of a Web site

  code is as follows copy code

$ (function () {
$ ("[placeholder]"). focus (function () {
var input = $) );
if (input.val () = = Input.attr ("placeholder")) {
Input.val ("");
Input.removeclass ("placeholder");
}
}). blur (function () {
var input = $ (this);
if (input.val () = "" | | Input.val () = = Input.attr ("placeholder")) {
Input.addclass ("placeholder");
Input.val (Input.attr (" Placeholder "));
}
}). blur ();
$ ("[placeholder]"). Parents ("form"). Submit (function () {
$ (this). Find ("[placeholder]"). each (function () {
var input = $ (this);
Input.val (input.val () = = Input.attr ("placeholder")? "": Goog.string.htmlEscape

(Input.val ());
})
);
});

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.