Skip to main content

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.

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.

I am very new to jQuery, and have recently converted one javascript function to jQuery. This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

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 funtion to select the target text within sentence div.

Javascript

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 am concerned whether I am doing everything right?, please let me know.

I'm very new to jQuery, and have recently converted one JavaScript function to jQuery. This function is part of a small utility that mimics click and drag for selection of text. Complete code is available on my github repo. Following is the screenshot of the utility.

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 function to select the target text within sentence div.

JavaScript

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'm concerned whether I'm doing everything right? Please let me know.

fixed the layout of the contents are recommended by community
Source Link
Imran Ali
  • 163
  • 2
  • 8

Need help converting pure javascript jQuery function to jqueryselect user specified text within a div

I am very new to jQuery, and have recently converted one javascript function to jQuery. This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

Screenshot of the utility

The purpose of the code is to allow user to select text input by entering the userdesired text in target text input, hitting the select button calls the getSelection funtion to select the target text within sentence div.

Javascript

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

This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

Screenshot of the utility

Question Having little knowledge of jQuery I am concerned whether I am doing everything right?, please let me know.

Need help converting pure javascript to jquery

I am very new to jQuery, and have recently converted one javascript function to jQuery. The purpose of the code is to select text input by the user in target text input within sentence div.

Javascript

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

This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

Screenshot of the utility

Question Having little knowledge of jQuery I am concerned whether I am doing everything right?, please let me know.

jQuery function to select user specified text within a div

I am very new to jQuery, and have recently converted one javascript function to jQuery. This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

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 funtion to select the target text within sentence div.

Javascript

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 am concerned whether I am doing everything right?, please let me know.

updated question to provide additional information
Source Link
Imran Ali
  • 163
  • 2
  • 8

I am very new to jQuery, and have recently converted one javascript function to jQuery. The purpose of the code is to select text input by the user in target text input within sentence div.

Javascript

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

This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

Screenshot of the utility

Question Having little knowledge of jQuery I am concerned whether I am doing everything right?, please let me know.

I am very new to jQuery, and have recently converted one javascript function to jQuery.

Javascript

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

This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo

Question Having little knowledge of jQuery I am concerned whether I am doing everything right?, please let me know.

I am very new to jQuery, and have recently converted one javascript function to jQuery. The purpose of the code is to select text input by the user in target text input within sentence div.

Javascript

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

This function is part of a small utility that mimics click and drag for selection of text. complete code is available on my github repo. Following is the screenshot of the utility.

Screenshot of the utility

Question Having little knowledge of jQuery I am concerned whether I am doing everything right?, please let me know.

Source Link
Imran Ali
  • 163
  • 2
  • 8
Loading