EditLive! 9 Documentation : getSelectedText Method

This method retrieves the currently selected text within the EditLive! applet. The method will retrieve the currently selected text and any inline tags within the current selection; it will not retrieve the parent tags of the selected text.

Prior to version 8.1 this function had no return value and the function was asynchronous. While asynchronous use is still supported, since version 8.1 this function can now be used synchronously with a return value.

Syntax

JavaScript
String editliveInstance.getSelectedText([jsFunct]);

Parameters

jsFunct

This parameter is optional.

The JavaScript function which receives the retrieved selected text.

Returns

The selected text.

Example Synchronous (EditLive 8.1+)

Calling getSelectedText synchronously returns the currently selected text (including inline tags) as a string. This example retrieves the currently selected text and then saves the retrieved contents to a <textarea> with the name selectedText. The name of the EditLive applet is editlive_js.

document.exampleForm.selectedText.value = editlive_js.getSelectedText();

Example Asynchronous (EditLive 8.0+)

When calling getSelectedText asychnronously you must provide a JavaScript function as the callback parameter for the getSelectedText method. In this example the retrieveSelectedText callback function receives the currently selected text (including inline tags) as a string. The name of the EditLive applet is editlive_js.

editlive_js.getSelectedText('retrieveSelectedText');

The JavaScript function specified as the callback parameter receives the contents of the editor and then saves the retrieved contents to a <textarea> with the name selectedText.

function retrieveSelectedText(src){
		document.exampleForm.selectedText.value = src;
	}

Remarks

This method is designed for use when selecting text within a single parent block tag. Using the method outside of this context may result in unexpected behaviour.

If using this method with selections which span multiple block tags, the opening parent tag of the block at the start of the selection and the closing parent tag of the block at the end of the selection will both be stripped from the retrieved content. For example:

If the following markup existed inside an instance of EditLive!:

<p>This is the START first paragraph</p>
<p>This paragraph is the second paragraph</p>
<p>This is the <b>third</b> paragraph and END contains some bold text</p>

And the selection extended from the word START in the first paragraph to the word END in the last paragraph, the following content would be returned:

START first paragraph</p>
<p>This paragraph is the second paragraph</p>
<p>This is the <b>third</b> paragraph and END contains some bold text