tags:

views:

63

answers:

4

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.

+2  A: 

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

Pablo Santa Cruz
That won't help him get the selected text though.
Andy E
+1  A: 

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/

sgriffinusa
Your solution will empty the field when the user clicks on it. I'm trying to get the text that is selected in the field, not make the field empty when it's selected.
Gabriel
The first solution will do what you asked (I added an example). The second was another option that may solve the problem in a different manner. I assume the "Enter your name" is text to help the user understand what the field is for. If this is the case, then removing that text when the user enters the field will eliminate the need to parse the "Enter your name" text out of the field.
sgriffinusa
+3  A: 

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/

kekekela
document.getSelection seems to be a step in the right way, but apparently it doesn't work in IE. Do you know if there's a way to make it work in there too?
Gabriel
@Gabriel try window.getSelection() or document.selection.createRange().text for IE, one or the other should work I think
kekekela
As I wrote in my edit, the IE version with createRange().text is working. Do you have any idea why the Firefox version is not working when the selected text is in an input box? Thanks!
Gabriel
@Gabriel have you tried window.GetSelection in firefox? As far as I know those are the only three ways to go about it, but for any given browser one of them *should* work.
kekekela
I tried with window.getSelection. It still is working on any "static" element. But it's not working on input text or textarea. I've googled a lot about getSelection and every example I find is done using static elements. So I guess I'll need to make my field a P or DIV instead and find some way to make it editable like it's done in Web-based text editors. Thanks for pointing me in the right direction ! I'll select your answer but if anyone finds how to make it work on an input text, I'd still like to know ! ;)
Gabriel
I managed to find the complete solution for this problem. I added it as an answer for further reference. Thanks a lot for your help!
Gabriel
@Gabriel Awesome man, glad you worked it out!
kekekela
+1  A: 

Ok here's the final answer. Looks like we have to use selectionStart and selectionEnd instead of getSelection() when we're working with input or textarea. Here's my test code:

$(".name").mouseup(function()
{
    var selected_text = "";
    if (window.getSelection) // Firefox
    {
        var text = $(this).val();
        selected_text = text.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd);
    }
    else // IE
    {
        selected_text = document.selection.createRange().text;
    }
    alert(selected_text);
});

And my HTML is simply :

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

So I finally got it. Thanks for everyone's input!

Gabriel