views:

169

answers:

2

I'm looking for a way to programatically select all the content inside a TinyMCE editor instance.

The reason I need this is that I'd like it if all the text inside the editor was selected, as soon as someone clicks on it (I'm using TinyMCE in conjunction with JEditable, by the way).

Thanks,
Edan

A: 

I just wrote something that allows you to progamatically select text inside an element(textarea/textbox etc.).

Maybe this can help :

function setSelection(elem,start,end){
  elem = document.getElementById(elem);
  if(elem){
    switch(arguments.length){
      case 1:
        elem.select();
        break;
      case 2:
        end = elem.value.length;
      case 3:
        if( elem.setSelectionRange ){
          elem.setSelectionRange(start,end);
        }
        else{ //IE branch
          var range = elem.createTextRange();
          range.moveStart("character",start);
          //IE measures moveEnd from end of string.
          // our function takes end coord from the start of the content itself.
          // for example, string of length 10 and you want to select to end at 8th character.
          // so you would give end=8 which is == -10 + 8 = -2
          range.moveEnd("character", -elem.value.length + end);
          range.select();
        }
    }
  }
};
Rajat
This isn't relevant to a TinyMCE editor instance.
Tim Down
you guys are ruthless. i added "maybe" to my case. anyways, downvote accepted. :)
Rajat
I do feel a bit guilty now. Sorry.
Tim Down
+1  A: 

Assuming you have your TinyMCE editor instance stored in a variable called ed:

ed.selection.select(ed.getBody(), true);
Tim Down
Thanks! I ended up adding a setup function in the tineMCE.init() settings, which did exactly this.
Edan Maor