How can I select programmatically from A(x1,y1) to B(x2,y2) ?
x1, y1, x2, y2 are pixel coordinates. I searched a lot and in all functions I found, we had to specify a specific tag and then it selects its content.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do this in current versions of all browsers. These browsers have at least one of the following:
document.caretPositionFromPoint()document.caretRangeFromPoint().TextRangeobject, which has amoveToPoint()method that takes pixel coordinates. However, it seems thatmoveToPoint(), which is used in all version of IE, can be buggy (see here and here, for example); I’ve simply been lucky that has worked in all the documents I’ve used it in.However, Mozilla does not yet implement any of these and neither does Opera, so this can’t be done in those browsers yet.Firefox 20 and later supports
document.caretPositionFromPoint(). Opera 15 supportsdocument.caretRangeFromPoint()Here’s some example code. It works in IE 5+, WebKit from around 2010 onwards, Firefox >= 20 and Opera >= 15.
Live demo: http://jsfiddle.net/timdown/ABjQP/
Code: