I amI'm very new to jQuery, and have recently converted one javascriptJavaScript function to jQuery. This function is part of a small utility that mimics click and drag for selection of text. completeComplete code is available on my github repo. Following is the screenshot of the utility.
The purpose of the code is to allow user to select text by entering the desired text in target
text input, hitting the select
button calls the getSelection
funtionfunction to select the target text within sentence
div.
JavascriptJavaScript
getSelection = function(){
sentence = document.getElementById("sentence");
target= document.getElementById("target");
selection = window.getSelection();
range = document.createRange();
index = sentence.innerHTML.indexOf(target.value);
range.setStart(sentence.firstChild,index);
range.setEnd(sentence.firstChild,(index + target.value.length));
selection.removeAllRanges();
selection.addRange(range);
}
jQuery
$(document).ready(function(){
$("#getSelection").click(function(){
sentence = $("#sentence")[0];
target= $("#target")[0];
selection = window.getSelection();
range = document.createRange();
index = sentence.innerHTML.indexOf(target.value);
range.setStart(sentence.firstChild,index);
range.setEnd(sentence.firstChild,(index + target.value.length));
selection.removeAllRanges();
selection.addRange(range);
});
});
Additional Info
Question: Having little knowledge of jQuery I amI'm concerned whether I amI'm doing everything right?, please Please let me know.