...

View Full Version : Get anchorNode ID



csam0003
03-19-2009, 12:37 AM
Hi guys,

I am programming in Firefox, and I wish to get the id of the achornode of the selected text.
I tried this but it doesnt work:
testArea = window.getSelection();
something = testArea.anchorNode.id;
any idea?
Thanks

TinyScript
03-19-2009, 05:19 AM
http://javascript.royh.cn/selection/anchornode-2-0-focusnode.html

Return a reference to the node where the user started (anchor) and ended (focus) the selection. Most typically, these are text node types. If the selection is set or extended via the addRange( ) method, these properties point to the node boundaries of the most recently added range.

Example


var anchor = selectionRef.anchorNode;
if (anchor.nodeType == 3 && anchor.parentNode.tagName == "td") {
// process selection start inside a table cell
}

TinyScript
03-19-2009, 05:37 AM
Wow, amazing what a little google can dig up.
The ondblclick didn't work good so I switched it to onmouseup and it's almost perfect. You need a toggle. I'd make some button you click to begin. use a boolean at the begining of the function. If the button is clicked your boolean is true and the function will work when you want it to instead of every time a mouseup event happens. button off and boolean is false.

it's easy. you can do it from an image even just keep the boolean global.
onclick="boolean=true"



<html>
<head>
<title>Selected Text Position</title>
<script language="javascript">
function searchword(evt) {
if (!evt) { evt = window.event; }
var srcText = null;
if (navigator.appName!='Microsoft Internet Explorer') {
var t = document.getSelection();
srcText = evt.target.innerHTML;
findPos(srcText, t);
}
else {
srcText = evt.srcElement.innerHTML;
var t = document.selection.createRange();
if(document.selection.type == 'Text' && t.text>'') {
document.selection.empty();
findPos(srcText, t.text);
}
}
}
function findPos(srcText, text) {
var spos = srcText.indexOf (text);
var epos = spos + text.length -1;
window.status = 'Start Position ' + spos + ' End Position ' + epos;
alert ('Start Position ' + spos + '\n End Position ' + epos);
}
function od(t) {
while (t.substr(t.length-1,1)==' ')
t=t.substr(0,t.length-1);
while (t.substr(0,1)==' ')
t=t.substr(1);
if (t) window.location.href = 'http://www.google.com/search?hl=en&q='+escape(t);

/*onDblClick="searchword(event);"*/
}
</SCRIPT>
</head>
<body onmouseup="searchword(event);">
<span>Hello this is a Javascript function to get the start and end position of highlighted text on a page</span> <br>Double click to get information about the start and end position. Watch the status bar.


</body>
</html>

csam0003
03-19-2009, 12:57 PM
thank you for your reply but I think you misunderstood me or maybe I have;)

Basically I have a function that highlights the selected text in any webpage. In order to do that, I need to get the current ID of the anchor node of the selected text.

So I have this code

workArea = document.getElementById('subjectMatter');

however the element id changes from page to page.

So I need the anchor id of testArea = content.window.getSelection();

TinyScript
03-19-2009, 09:04 PM
thank you for your reply but I think you misunderstood me or maybe I have;)

Basically I have a function that highlights the selected text in any webpage. In order to do that, I need to get the current ID of the anchor node of the selected text.

So I have this code

workArea = document.getElementById('subjectMatter');

however the element id changes from page to page.

So I need the anchor id of testArea = content.window.getSelection();

then give the anchors each an id. I don't know what you mean. You can't expect an anchor to magically have an id. Perhaps you need some form of parentNode childNode or something. I'm not up to that stuff just yet, but google works like a charm with search words

jrmayberry
12-09-2009, 04:13 AM
I know this is an old post but I had this same question and couldn't stand to leave this hanging. I actually joined CodingForums just so I could share my answer.

To get the anchor id use: "X.anchorNode.parentNode.id"
To get the focus id use: "X.focusNode.parentNode.id"

where X is the var for your selection/range object.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum