get the focus of the cursor

8 posts, 1 answers
  1. Henrik
    Henrik avatar
    47 posts
    Member since:
    Dec 2010

    Posted 16 May 2011 Link to this post

    I tried to find a method to get the actual position of the cursor in the editor but I was not able to. The method setFocus is available but getFocus does not occur. Is there a solution for a very helpful method?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 May 2011 Link to this post

    Hello Henrik,

    One suggestion is to use the editor PasteHtml client-side method, which pastes HTML content into the cursor position.
    Take a look at the following help article for more on this.
    pasteHtml

    Thanks,
    Princy.
  3. Rumen
    Admin
    Rumen avatar
    13804 posts

    Posted 18 May 2011 Link to this post

    Hi Henrick,

    You can get the current caret position by using browser-specific methods. In fact the editor can assist you nicely here, because you can use another of its cross-browser methods:
    var range = editor.getSelection().getRange();

    This range object is specific for each browser, and its API will differ greatly, so from this point on you will need to write browser-specific code.

    You can also see the following articles which provide useful information about caret position, moving and locating the carer position:
    Mouse Cursor Position
    How to set/get caret position of a textfield in IE?
    Working with the Cursor Position.
    http://www.wrox.com/WileyCDA/Section/JavaScript-DOM-Ranges-Page-2.id-292302.html
    http://www.quirksmode.org/dom/range_intro.html
    http://msdn.microsoft.com/en-us/library/ms535872%28VS.85%29.aspx

    To obtain a reference to the editor document use editor.get_document().

    All the best,
    Rumen
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  4. Henrik
    Henrik avatar
    47 posts
    Member since:
    Dec 2010

    Posted 23 May 2011 Link to this post

    Does the method "getSelection" also work if no selection is made?
  5. Rumen
    Admin
    Rumen avatar
    13804 posts

    Posted 24 May 2011 Link to this post

    Hi Henrik,

    If the selection is empty then the getSelection method will return an empty selection.

    Regards,
    Rumen
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  6. Henrik
    Henrik avatar
    47 posts
    Member since:
    Dec 2010

    Posted 25 May 2011 Link to this post

    Yeah. I guess this is not what I want to have... I want to program the behavior like the align buttons. They work like this:
    • set the cursor in the paragraph
    • click any align button
    • the whole paragraph changes its align
    So I want to have this behavior:
    • set the cursor in the paragraph
    • click any line distance button (the method searches itself for the nearest <p> elements and sets a css class)
    • the whole paragraph changes its line distance

    I also implented such functionality but it is not very comfortable. You have to double click the paragraph... I only want to get the cursor position in the content area maybe like this:
    If this text is inside the content area:
    "THIS IS SAMPLE TEXT"
    and the cursor is behind the M then I want to start a method called getCursorPosition that returns in this case 11. But I guess this is not possible but essential.
  7. Henrik
    Henrik avatar
    47 posts
    Member since:
    Dec 2010

    Posted 17 Jun 2011 Link to this post

    Any ideas?
  8. Answer
    Rumen
    Admin
    Rumen avatar
    13804 posts

    Posted 20 Jun 2011 Link to this post

    Hi Henrik,

    You can check whether the cursor is placed inside a <p> tag using the

    if (editor.getSelectedElement().tagName == "P")
    {
           
    }

    and if it is to apply the desired CSS inline attribute to the selected P tag.

    Note that the FixEnclosingP filter of RadEditor removes a parent paragraph tag if the whole content is inside it. This is because IE automatically wraps the empty content area in a <p> tag which misleads the validators that the editor is not empty. In such scenarios you should check whether the getSelectedElement() element is the <body> tag and if it is to create a <p> tag and put the body's content in it using the DOM methods.

    Best regards,
    Rumen
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Back to Top