How to put into a variable only the text that was selected within a textarea?

Asked

Viewed 1,607 times

4

With

var texto = document.getElementById('texto').value;

I get everything that’s in the tetxarea that has this id, but what if I want a partial text?

For example the whole text is this:

Germany bought the game against Brazil

If I just select "bought the game", I would like my var to have this content.

Added (11/5/15):

Well, with "mouseup" Alert works, but so, if I simply need to put the value of the selection within a variable with

 var textoSelecionado = showSelection(this);

and then try to use this value?

My ultimate goal is to be able to put HTML tags around the selected text.

However if I click on the link/image/button that performs this function automatically the text is no longer selected and so I tested the function "showSelection" is no longer effective.

I figured out a way to make it functional: enabling and disabling the buttons, so if they are active when selecting the text in the "mouseup" it undergoes the changes. If they don’t have assets they don’t change. It would be like controlling whether or not to use the showSelection function. This I think I can do, but I would like to retain the selected text to use it by clicking on a button or link.

  • 1

    https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement#Insert_HTML_tags_example

  • Cool, but is cross browser? by what I saw in the box is not marked most browsers. Unless we and refer to this.

  • 1

    I believe it works from IE9 since the properties are used selectionStart and selectionEnd and the method setSelectionRange.

  • This is not a problem, but in other browsers it works?

  • 1

    Yes, other browsers (firefox, Chrome, safari, opera) support these features (I just don’t know how many mobile versions).

  • I’ve even adapted the scheme that I commented on the question to activate and deactivate the buttons, but this alternative that you showed may be more practical. Thank you, I’ll test.

  • @Oeslei worked on all nabegadores, thanks, as what I’m building is for use of Adm, even if it doesn’t work on older browsers, just ask the user to update all browsers to latest version.

Show 2 more comments

1 answer

5


I found an answer on Soen which I have adapted. So with this function you can retrieve what is selected.

HTML

<textarea name="" id="texto" cols="30" rows="10">Texto de exemplo</textarea>

JS

function showSelection(textComponent) {
    var selectedText;
    // IE version
    if (document.selection != undefined) {
        textComponent.focus();
        var sel = document.selection.createRange();
        selectedText = sel.text;
    }
    // Mozilla version
    else if (textComponent.selectionStart != undefined) {
        var startPos = textComponent.selectionStart;
        var endPos = textComponent.selectionEnd;
        selectedText = textComponent.value.substring(startPos, endPos)
    }
    return selectedText;
}

var textarea = document.getElementById('texto');
textarea.addEventListener('mouseup', function () {
    var textoSelecionado = showSelection(this);
    if (textoSelecionado) alert(textoSelecionado);
});

jsFiddle: http://jsfiddle.net/a1tc9984/

  • I know this, but I don’t want the whole text, I want it partially.

  • 1

    @Iwannaknow changed the answer to only give the selected text.

  • 1

    Thank you, I’ll study it and I’ll be right back.

  • I had already seen some of these functions and read about this problem that there is nothing crossbrowser, but I read in old Google pages, I thought that at the present time there would be something that would solve this. I added a text to the question.

  • 1

    @Is this what you want? -> http://jsfiddle.net/tLbysg4p/

  • I’ve already done that. After you showed the functions became easy and I even think it is a viable solution, but I talked about selecting a text and modifying it by clicking a button.

  • I used "replace" and also active or deactivate a button so that this function only works if the button of this tag is enabled.

  • Did you see the link @Oeslei posted? https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement#Insert_html_tags_example

  • @Iwannaknow yes, I saw it. This is a technology that is not yet implemented in browsers. It may be that in a few years it is common to use. For now I only know the way I put it.

  • I appreciate it was already of great help, but I tested here and it worked the link technology.

  • @Iwannaknow also tests in IE version 9, 10 and 11, and tests Safari (Mac) and Firefox. I think it will not work in most of these.

  • Works on all these: Chrome, FF, IE 11, Safari and Opera. I can’t test on older versions as I update all browsers.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.