Find Selected DOM Element

  •  05-05-2011, 2:04 AM

    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:
View Complete Thread