HTML and CSS Reference
In-Depth Information
var selection =
window.getSelection();
var range =
selection.getRangeAt(0); var
contents =
range.extractContents();
We're not going to cover the text-
selection API in detail here. If you
want to find out more, check out
the specs.
Now you can insert the selection
back into the document inside a
<section> element. The current
selection is replaced with the new
content:
execCommand(
'inserthtml',false,
'<section>' +
contents.textContent +
'</section>');
Finally, a common requirement when editing web pages is inserting
links. The command for this is CreateLink , but you also need to provide
the user with a way of entering the link as well as the button to apply it:
if (
document
.getElementById('theURL')
.checkValidity()
) {
execCommand(
'CreateLink',
false,document
.getElementById('theURL')
.value
);
}
Search WWH ::




Custom Search