Set the default input focus in JavaScript (js)

Source: Internet
Author: User

This function is often used in reply and reference, that is, click reply or reference. For example, if the input focus is displayed in the message input box, if you use an anchor to locate the input focus, the input focus cannot be activated. Copy codeThe Code is as follows: javascript: document. getElementById ("id"). focus ();
Or javascript: document. all. id. focus ();
Or javascript: document. all. name. focus ();

Example:Copy codeThe Code is as follows: <input type = "text" name = "tt" id = "tt">
<Input type = "button" name = "bt" id = "bt" onclick = "javascript: document. all. tt. focus (); "value =" set focus for text box ">

Next, let's take a look at the location of the reply and reference used in this blog, and how to locate the focus on the right side of the text.Copy codeThe Code is as follows: // reference the call Function
Function quote (name, id ){
Var quoteMsg = document. getElementById (id). innerHTML;
Var content = document. getElementById ("comment ");
QuoteMsg = quoteMsg. replace (//g ,"~ ^ "). Replace (/<BR>/g, 'n ');
Content. focus (); // to focus on the right side of the text, this line of code should be in front
Content. value = quoteMsg + '[Reference' + name + ']' + 'n' + '--------------------' + 'n' '; // enter the text
Return false; // The role of canceling href
}
// Reply to the automatically input name Function
Function backcomment (msg ){
Backdb = document. getElementById ('comment ');
Backdb. focus ();
Backdb. value = msg + 'n ';
Return false;
// Return true; originally positioned with an anchor
}

Related Article

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.