Home Web Front-end JS Tutorial JavaScript method to get selected text in text box_javascript skills

JavaScript method to get selected text in text box_javascript skills

May 16, 2016 pm 04:13 PM
javascript text box method Obtain

The example in this article describes how to obtain the selected text in the text box using JavaScript. Share it with everyone for your reference. The specific analysis is as follows:

The code here can be used to obtain the selection selected by the user in the text input box or textarea through the mouse.
Need to pay attention to the problem of ie.

The code is as follows:

Copy code The code is as follows:
<script type="text/javascript">
function getFieldSelection(select_field)
{
Word='';
If (document.selection) {
         var sel = document.selection.createRange();
If (sel.text.length > 0) {
                word = sel.text;
}
}
​ else if (select_field.selectionStart || select_field.selectionStart == '0') {
        var startP = select_field.selectionStart;
        var endP = select_field.selectionEnd;
If (startP != endP) {
                 word = select_field.value.substring(startP, endP);
}
}
Return word;
}
</script>
<textarea id="a" rows="3" cols="20">Select me and click the button below</textarea><br />
<button onclick="alert(getFieldSelection(document.getElementById('a')))">button_click</button>
<br />
<strong>

Note: When using non-button elements to trigger the onclick event in IE, no result will be obtained. Because in IE, when a non-button element is clicked, the selection content in the entire page will be changed. This problem does not exist under Firefox and Opera. So the above (Span_onclick) will not be available under IE.

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

The difference between Go language methods and functions and analysis of application scenarios The difference between Go language methods and functions and analysis of application scenarios Apr 04, 2024 am 09:24 AM

The difference between Go language methods and functions and analysis of application scenarios

See all articles