tags:

views:

4

answers:

1

Hi All! I need to add a span at caret position in my application. I could add span at caret position with following code, but could not position the caret inside it, so that if user types in it will go inside the new span.

marker = ed.selection.getBookmark();
ed.selection.moveToBookmark(marker);
tinyMCE.execCommand('mceInsertContent',false,'<span id="mytitle"></span>');
ed.selection.moveToBookmark(marker); 

I have spent 1and 1/2 day for this. Kindly suggest. Thanks in advance.

A: 

It took me some time to place a cursor myself. You might need to modify it slightly to be used with the span you created, but this should put you intio the right direction:

// sets the cursor position to the defined node
// ed: editor, start: defines if the cursor is to be placed at the start or end of the node
// return node: boolean, if set returns the caretnode instead of deleting it
function setCursor(ed, node, start, return_node){

  tn = ed.getDoc().createTextNode(".");
  if (start){
    node.insertBefore(tn, node.firstChild);
  } 
  else node.appendChild(tn);

  rng = ed.selection.getRng();
  rng.selectNode(tn);
  rng.setStartBefore(tn);
  rng.setStartAfter(tn);

  ed.selection.setRng(rng);

  if (return_node) return tn;

  node.removeChild(tn);

}

Thariama