Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I retrieve the user selected text in a text form input?

Tags:

jquery

I'm trying to get only the selected part of an input string that was selected by the user. Let's say I have this input :

<input type="text" name="first_name" id="first_name" value="Enter your name" />

And the user selects the "Enter" word by clicking and dragging in the text field. Can I retrieve that text with jQuery?

I know I can get the whole value this way :

$("#first_name").val();

But so far, I didn't find anyway to get only the selected "Enter" part. Anyone is able to point me in the right direction? Thanks!

EDIT

I tried document.getSelection and it's working great in Firefox on a static text element such as a "p". It's not working for me so far on a text input.

In IE, window.selection.createRange().text is working both on a "p" and in a text input.

like image 664
Gabriel Avatar asked Aug 05 '10 18:08

Gabriel


People also ask

How do I get text content of input?

We can get the value of the text input field using various methods in JavaScript. There is a text value property that can set and return the value of the value attribute of a text field. Also, we can use the jquery val() method inside the script to get or set the value of the text input field.

How do you get the data from the textbox which is inputted by the user?

To extract the information which a user enters into the text fields using Javascript, the Javascript code to do would be: The line document. getElementById("firstname"). value accesses the text box with an id of firstname and retrieves the value which the user has entered into this text box.

How will you know that a text is selected?

To select text, click and drag your cursor across the text. The text that is highlighted in gray is selected text. Selected text can be copied, cut, or pasted. Many features and settings in Microsoft Office are applied only to text that is selected.

What is a text input element?

<input>: The Input (Form Input) element. The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent.


3 Answers

Should be able to do this using jQuery's select function and document.getSelection

http://www.devguru.com/Technologies/ecmascript/quickref/doc_getselection.html http://api.jquery.com/select/

like image 69
heisenberg Avatar answered Nov 08 '22 19:11

heisenberg


This might help. You will need to use .select() function.

like image 21
Pablo Santa Cruz Avatar answered Nov 08 '22 19:11

Pablo Santa Cruz


You could use the regular javascript replace method.

$("#first_name").val().replace("Enter your name", "")

See an example: http://jsfiddle.net/gezDF/

However, another option could be to remove the text when the user entered the field. This can be done with something like the following:

$(document).ready(function(){
    $("#first_name").focus(function () {
         $(this).val("");
    });
}​);​

See an example: http://jsfiddle.net/JNmAF/

like image 1
sgriffinusa Avatar answered Nov 08 '22 19:11

sgriffinusa