Sunday, 25 August 2013

Using mouse cursor position as a range starting point in CoffeeScript/JavaScript

Using mouse cursor position as a range starting point in
CoffeeScript/JavaScript

As the title states, I would like to use my cursor's position as the start
point to a range.
Right now have simple sample like this
<html>
.
.
<p>The quick brown fox jumps over the lazy dog</p>
.
.
</html>
On the CS/JS side of things I have event listen set to mouse move that
attempts to print out the offset for the cursor position, however I am not
using the correct method and end up getting either undefined or no method
errors.
Again, really simple CS for the time being since I really just wanted to
test it out.
$(document).ready ->
$(document).mousemove ->
target = event.target
console.log("#{target.offset()}") // also tried .rangeOffset .offset
Ideally I would like something that I can input into a range.setStart()
function.
For example, if I was to be moused over the f in fox I would want the
offset to be return as 16 so that I may then set the start of the range
like so range.setStart(target,16).
Any help setting me in the right direction would be greatly appreciated.

No comments:

Post a Comment