Add default text implementation code to the jquery search box

Source: Internet
Author: User

I will see a prompt before entering text in the search box of many websites. The prompt will be automatically hidden when we click to enter. Below I will introduce two implementation methods.

For example, use js's focus loss and focus Retrieval Method

Js Code

The Code is as follows: Copy code

<Input type = "text" name = "s" value = "prompt text" onfocus = "if (value = 'prompt text ') {value = ''}" onblur = "if (value ='') {value = 'prompt text'} "/>

Implementation 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 () = 'enter a keyword! '){
$ (This). val ("");
}
});
$ ('# Keyword'). blur (function (){
If ($ (this). val () = ""){
$ (This). val ('enter the keyword! ');
}
});
});


A website instance

The Code is as follows: Copy code

$ (Function (){
$ ("[Placeholder]"). focus (function (){
Var input = $ (this );
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.html Escape

(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.