This is a migrated thread and some comments may be shown as answers.

get the focus of the cursor

7 Answers 73 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Henrik
Top achievements
Rank 1
Henrik asked on 16 May 2011, 11:48 AM
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?

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 May 2011, 02:19 PM
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.
0
Rumen
Telerik team
answered on 18 May 2011, 02:06 PM
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.

0
Henrik
Top achievements
Rank 1
answered on 23 May 2011, 10:43 AM
Does the method "getSelection" also work if no selection is made?
0
Rumen
Telerik team
answered on 24 May 2011, 04:14 PM
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.

0
Henrik
Top achievements
Rank 1
answered on 25 May 2011, 11:26 AM
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.
0
Henrik
Top achievements
Rank 1
answered on 17 Jun 2011, 10:15 AM
Any ideas?
0
Accepted
Rumen
Telerik team
answered on 20 Jun 2011, 02:30 PM
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.

Tags
Editor
Asked by
Henrik
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rumen
Telerik team
Henrik
Top achievements
Rank 1
Share this question
or