Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    45
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Get anchorNode ID

    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

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    http://javascript.royh.cn/selection/...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
    }

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    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"

    Code:
    <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>

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    45
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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();

  • #5
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    Quote Originally Posted by csam0003 View Post
    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

  • #6
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ID for anchorNode/focusNode

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •