Jacob Phillips
Jacob Phillips

Reputation: 9274

Select (highlight) text in Dart

This question is fairly fundamental but I can't find a solution anywhere. There are many examples of highlighting text in javascript with or without jQuery, but I'm having trouble figuring it out in Dart.

The Selection and Range classes appear to support this but the documentation is kind of sparse.

Upvotes: 1

Views: 832

Answers (1)

Jacob Phillips
Jacob Phillips

Reputation: 9274

Okay while trying to write the question thoroughly I figured it out. It's actually quite simple.

HTML: <p id='text'></p>

Dart:

var text = querySelector('#text');

Using Selection only:

window.getSelection().selectAllChildren(text);

Using Range and Selection:

var range = document.createRange();
range.selectNodeContents(text);
window.getSelection().addRange(range);

Both examples select all of the text inside the node. For more information on how to use Range, see this answer.

Upvotes: 1

Related Questions