TX Text Control Blog

HTML5: Using the new JavaScript Selection object

Björn Meyer

Blogged by Björn Meyer on December 24, 2016 and tagged with release, javascript, html5.


Version X14 (24.0) introduces a new JavaScript object to handle the position and length of a text selection. It can be also used to replace the current selection with text in a certain format.

The Selection object can be retrieved and set through the selection property of the TXTextControl object. The following JavaScript code snippet gets the current Selection and writes the current selection start and length values to the console:

The getBounds method takes a callback function with one parameter as a parameter. The callback function is called with a SelectionBounds object as a parameter.

In order to set a new selection in the document, the setBounds method is used. This method takes a SelectionBounds object as a parameter:

When a selection should be replaced with plain or formatted ext, the text needs to be selected first. After that, the load method can be used to replace the selected text.

The following code snippet selects text and inserts plain text into the selection:

 
 
Call us: +1 704-544-7445 (United States)
 
 

Products

Support

Downloads

Corporate

Buy Now

About Text Control

Founded in 1991, Text Control is an award-winning Visual Studio Industry Partner and leading vendor of word processing and reporting components for Microsoft development technologies. The products help thousands of developers add comprehensive word processing functionality to their applications. Text Control is 100% self-owned and independent of all investor decisions. Customers benefit from these years of experience, large user-base, and at the same time, appreciate developing with a mature, reliable product.

SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100SD Times 100Visual Studio 2015 Launch PartnerVisual Studio 2013 Launch PartnerVisual Studio 2012 Launch PartnerVisual Studio Partner