Log in or register to post comments
Last post
littlened's picture
Joined: 09/12/2007
Posts: 4
Insert text at cursor position?
Is there anyway to insert text at the cursor position? I've ready bit and peices on google but some say it doesnt work in IE7.
randym56's picture
Joined: 25/01/2008
Posts: 2
Re: Insert text at cursor position?
After many hours of searching I can't seem to find the answer anywhere. I don't want to use tinyMCE, but it does give me the ability to insert a text field selected "on click" from within the same form.

I think it has to do with this function:
FCKeditor.prototype._InsertHtmlBefore = function( html, element )

I am not clear about how to construct the syntax so that it works. Here is the line I attempted...

<tr>
<td>
<?PHP echo _("Contact") . "<BR>" . $contacts_menu; ?>
</td>
<td><a href="javascript:void(0);" onClick="FCKeditor.prototype._InsertHtmlBefore('{'+document.forms[0].email_template_body.value+'}','{'+document.forms[0].contacts_fields.value+'}');"><?php echo _("Add");?></a></td>
</tr>


Here is the setup for the FCKeditor field:
<?php
$oFCKeditor = new FCKeditor('email_template_body') ;
$oFCKeditor->BasePath = $http_site_root.'/include/fckeditor/'; //$include_directory . 'fckeditor/' ;
$oFCKeditor->Value = $email_template_body ;
$oFCKeditor->Height = '300';
$oFCKeditor->Create() ;
?>

In tinyMCE when add button is selected, the contact_fields item selected in the dropdown is inserted into the textarea where the cursur was last selected.

Thanks in advance!
alfonsoml's picture
Joined: 31/12/2006
Posts: 3742
Re: Insert text at cursor position?
It's very easy, use the API: http://wiki.fckeditor.net/Developer%27s ... script_API and call the InsertHtml
randym56's picture
Joined: 25/01/2008
Posts: 2
Re: Insert text at cursor position?
It is not as "simple" as you think. After more than 4 hours of hacking to try to make everything I have read work in PHP it still does not work.

I admit I am a novice when it comes to javascript, but it baffles me that I can't find a "simple" example for inputting dynamic text using PHP in FCKeditor on the internet anywhere.

I am able to replicate the example8.html but that doesn't help me because the text I am trying to insert is dynamic in the form. It would be really useful if you could point me to a place where I can get an example that works.

Thanks!
alfonsoml's picture
Joined: 31/12/2006
Posts: 3742
Re: Insert text at cursor position?
The key is that you can't do that in PHP, it must be done in javascript. So use sample8 as a base and set the javascript string from the php code, and then use javascript to insert that string.
donseba's picture
Joined: 30/01/2008
Posts: 8
Re: Insert text at cursor position?
I'm trying to do the same thing, basically I have pre-formatted sets of text that i want to add to the text editor by clicking on a link, I have tried this, but as expected it did not work
$buff.=  "<a href=\"javascript:oEditor.InsertHtml('hello');\">hello</a>
this gives an error, oEditor is not defined , I'm also using fckeditor with PHP , it would be really nice to get some help with this , because it is vital for a function i'm working on...

thanks for the help.
alfonsoml's picture
Joined: 31/12/2006
Posts: 3742
Re: Insert text at cursor position?
From sample08:
function InsertHTML()
{
   // Get the editor instance that we want to interact with.
   var oEditor = FCKeditorAPI.GetInstance('FCKeditor1') ;

   // Check the active editing mode.
   if ( oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
   {
      // Insert the desired HTML.
      oEditor.InsertHtml( '- This is some <a href="/Test1.html">sample<\/a> HTML -' ) ;
   }
   else
      alert( 'You must be on WYSIWYG mode!' ) ;
}
donseba's picture
Joined: 30/01/2008
Posts: 8
Re: Insert text at cursor position?
to alfonsoml

sorry for my previous post, I didn't take a look at example 8, once I had seen that it all got clear to me, tnx again and sorry for the stupid post!! :oops: