Find Selected DOM Element

Last post 05-05-2011, 3:27 PM by dday. 2 replies.
Sort Posts: Previous Next
  •  05-05-2011, 2:04 AM 67452

    Find Selected DOM Element

    I dont know where/if these things are posted, but there should be a place to post solutions to common issues (beyond the documentation).
     
    I wanted to be able to have access to the DOM element that is selected in the cute editor for IE, FF, Chrome. I couldn't find it in the forum which I thought was strange. With a bit of effort I updated some code I found to to accomodate this. I wasnt sure about a few of the checks and balances so I left them in there. Feel free to give this efficiency tweaks if you are more versed in this product.
     
    Otherwise I hope it can help someone.
     
    //returns selected dom element from editor
    function getSelectedElement()
    {
    var editor = document.getElementById(EditorAdsId);

    var rng=null,html;

     // get the active editor document
    var editdoc = editor.GetDocument();
     
    // get the active editor window
    var editwin = editor.GetWindow();
    if (document.selection && document.selection.createRange)
    {
    rng=editdoc.selection.createRange();
    if( rng.htmlText )
    {
    html=rng.parentElement();
    }
    else if(rng.length >= 1)//not sure about this code dday
    {
    html=rng.item(0).parentElement();
    }
    }
    else if (window.getSelection)
    {
    rng=editwin.getSelection();
    if (rng.rangeCount > 0 && window.XMLSerializer)
    {
    rng=rng.getRangeAt(0);
    html = rng.startContainer.parentNode;
    }
    }
    return html;
    }
    Filed under:
  •  05-05-2011, 1:58 PM 67457 in reply to 67452

    Re: Find Selected DOM Element

    Dear dday,
     
    rangeCount: The number of ranges within the selectionRange object.  In IE and FF 3+ one can select several contiguous sections [& thereby have multiple ranges] by holding the control key while selecting.   Furthermore, in all versions of FF, other ranges can be added using the addRange method.  In Webkit & Opera the rangeCount is always 1.
     
     
    Thank you for asking
  •  05-05-2011, 3:27 PM 67458 in reply to 67457

    Re: Find Selected DOM Element

    Eric, thank you for responding...
     
    Forgive me for being a little dense, but I'm just following up for clarity's sake.
     
    Im guessing your post is a response to my //comments in the code thus(this seems to make sense in context):
    else if(rng.length >= 1)//not sure about this code dday
     
    Or maybe you were just trying to help my obvious confusion. I hadn't really played with these methods before and so have no idea how many goofy things may be in there. All I know is it works like a charm. Either way thanks again.
     
    Worth noting that this method allows me to combat something I've seen a few others post about which is the "CssClass" toolkit control. In the current version this puts a span with the class inside whatever element is selected. This is completely undesirable on so many levels I think. So with a little extra effort I attach this code(plus a little extra) to the CssClass toolkit control and I get an actual class put on the element that is actually selected instead of a span inside. As a bonus I still retain all the other subtle functions of the CssClass control's behaviors.
     
    BTW...its ironic that the page you gave me for JavaScript advice is filled with JavaScript errors when you try to do anything with it in IE8 (thats no reflection on you...just ironic).
     
    Thx,
     
    dday
View as RSS news feed in XML