Jump to content

DOM method for identifying selection???


jeffman
 Share

Recommended Posts

Okay, I've been exploring ranges and tree walkers and a lot of far out stuff. Here's what I want: the user clicks in some text that is NOT in a regular text entry element. I want a reference to the click place. Not coords, not an object name. I want something that returns a reference to a text node and an index to the character.NOW, I can get this using NON-W3C methods, like window.getSelection() in the case of gecko clients and document.selection.createRange() in the case of IE.With that reference, I can do magical things, and I see a path to even greater things.I simply can't believe that the DOM won't say, "Oh, yes, here you are."I guess all I'm asking for now, from those who've studied such arcana, is confirmation that this is the way things are. OR, a genuine DOM thingamajig.

Link to comment
Share on other sites

Sorry, Jesh, been there. PPK just does what I'm already doing, which might be my answer . . .JSG, I suppose it would return the nearest text node and an index to zero or the last char, depending on where you are. It might return the container node and let you figure it out.This is an area where IE and W3/Gecko disagree straight up the line. It's just the start of the sequence that W3 offers nothing that I can find. Some goofer on a list I found was like, "Why would you even want that, heh heh?" I'm actually building a little WYSIWYG text editor, and I just hate that the user's first action can only be accessed noncompliantly.Why, you ask. Well, TinyMCE is not so tiny and it clanks a bit, on slower machines anyway, because it wants to do everything. I just want a eety-beety thing. Besides, I want to.

Link to comment
Share on other sites

Since you're just exploring, maybe this little script I threw together will help spark some ideas.Current limitations:1) Only works for one-line, inline elements.2) Only works with monospaced fonts.Besides that, it seems to work great. heh :)

<html><head><style>* { font-family: monospace; }</style></head><body><script type="text/javascript">document.onclick = handleClick;function handleClick(e){    e = e || window.event;    var el = e.target || e.srcElement;    switch(el.tagName.toLowerCase())    {        case "html":        case "body":           return;        default:           var x = e.clientX - el.offsetLeft;           var w = el.offsetWidth;           var l = el.innerHTML.length;           var textNode = el.childNodes[0];           var pos = Math.floor(x / (w/l));           var char = el.innerHTML.substr(pos,1);           alert("Did you click on '" + char + "'?");           break;    }}</script><span>This is some text.</span><br /><span>Here is more of similar text.</span></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...