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.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement#Insert_HTML_tags_example
– Oeslei
Cool, but is cross browser? by what I saw in the box is not marked most browsers. Unless we and refer to this.
– I Wanna Know
I believe it works from IE9 since the properties are used
selectionStart
andselectionEnd
and the methodsetSelectionRange
.– Oeslei
This is not a problem, but in other browsers it works?
– I Wanna Know
Yes, other browsers (firefox, Chrome, safari, opera) support these features (I just don’t know how many mobile versions).
– Oeslei
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.
– I Wanna Know
@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.
– I Wanna Know