Code for selecting textarea text in firefox compatible with javascript _ form special effects

Source: Internet
Author: User
JavaScript code mainly used to select textarea text compatible with firefox and ie

The Code is as follows:


Function getSelectedText (){
Var selectedText;
Var textField = document. getElementById ('inputtextarea ');
If (window. getSelection) selectedText = getTextFieldSelection (textField); // getTextFieldSelection (document. getElementById ("inputTextArea "));
Else selectedText = document. selection. createRange (). text;
Alert (selectedText );
}

Function getTextFieldSelection (e ){
// Var oEvent = arguments. callee. caller. arguments [0];
If (e. selectionStart! = Undefined & e. selectionEnd! = Undefined)
Return e. value. substring (e. selectionStart, e. selectionEnd );
Else return "";
}


<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> JavaScript code compatible with textarea text in firefox _ www.jb51.net </TITLE> <meta name = "Generator" CONTENT = "EditPlus"> <meta name = "Author" CONTENT = ""> <meta name = "Keywords" CONTENT =" "> <meta name =" Description "CONTENT =" "> <script type =" text/javascript "> function getSelectedText () {var selectedText; var textField = document. getElementById (' InputTextarea '); if (window. getSelection) selectedText = getTextFieldSelection (textField); // getTextFieldSelection (document. getElementById ("inputTextArea"); else selectedText = document. selection. createRange (). text; alert (selectedText);} function getTextFieldSelection (e) {// var oEvent = arguments. callee. caller. arguments [0]; if (e. selectionStart! = Undefined & e. selectionEnd! = Undefined) return e. value. substring (e. selectionStart, e. selectionEnd); else return "";} script <style type = "text/css"> ul {list-style: none ;} </style> </HEAD> <BODY> <ul> <li> methods for selecting textarea text in firefox </li> <textarea id = "inputTextarea" rows = "6" cols = "50"/> never trust what I said, I have never been so brave and so strong. I keep vowed to tell stories honestly, but how strong is my desire to tell the truth, and how much interference I have. I sadly found that the truth cannot be restored at all. My memory is always changed by my emotions, and then I am teased and betrayed. It's hard to argue whether it's true or not. </Textarea> </li> <button onclick = "getSelectedText (); "> obtain the selected text segment </button> </li> </ul> </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.