Great beginning, he is interested in the ability to choose text inside a div. Here is a suggestion:
HTML:
<div class="highlightText">
This paragraph contains some text. Try selecting a part of it to see something cool happen!
</div>
<button id="makeItCool">Create Magic</button>
CSS:
.highlightText {
margin-bottom: 2em;
}
button {
cursor: pointer;
}
.highlight {
background-color: blue;
}
JS:
if (!window.x) {
x = {};
}
x.Selector = {};
x.Selector.getSelected = function() {
var selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection();
} else if (document.getSelection) {
selectedText = document.getSelection();
} else if (document.selection) {
selectedText = document.selection.createRange().text;
}
return selectedText;
}
$(document).ready(function() {
$("#makeItCool").click(function() {
var chosenText = x.Selector.getSelected();
console.log(chosenText.focusNode);
if (chosenText.focusNode.length > 0) {
var paragraph = $(".highlightText").text();
paragraph = paragraph.replace(chosenText, '<span class="highlight">' + chosenText + '</span>');
$(".highlightText").html(paragraph);
} else {
}
});
});