get selected HTML Source Code

Looking for help? Ready to help? That's the place to go.

get selected HTML Source Code

Postby urslang » Mon Nov 10, 2008 2:19 pm

How can I get the selected HTML source code?

This code gets the selected text. I would like to get the HTML source code or the ID of selected HTML element.

Code: Select all
var selection = "";
if(editorInstance.EditorDocument.selection != null) {
   selection = "value1 "+editorInstance.EditorDocument.selection.createRange().text;
}
else {
   selection = "value2 "+editorInstance.EditorWindow.getSelection();
}
urslang
 
Posts: 2
Joined: Mon Nov 10, 2008 2:13 pm

Re: get selected HTML Source Code

Postby saul11 » Sat Nov 15, 2008 8:00 pm

IE offers a command that can get you the HTML of the selection I believe, for other browser I have a snippet... but not on this PC. PM me or reply on this topic to remind me to look it up for you.
Freelance webdeveloper, FCKeditor plugin developer. FCKeditor contributor.

FCKPlugins: http://www.saulmade.nl/FCKeditor/FCKPlugins.php
FCKSkins: http://www.saulmade.nl/FCKeditor/FCKSkins.php
FCKSnippets: http://www.saulmade.nl/FCKeditor/FCKSnippets.php
saul11
 
Posts: 633
Joined: Mon Jun 19, 2006 5:39 am
Location: NL

Re: get selected HTML Source Code

Postby brecht.conba » Wed Nov 19, 2008 2:34 pm

Hello, i'd also like to know a solution to this problem.

Thanks in advance.
brecht.conba
 
Posts: 1
Joined: Wed Nov 19, 2008 2:33 pm

Re: get selected HTML Source Code

Postby saul11 » Wed Nov 19, 2008 3:05 pm

Hi, thanks for reminding. The function to get the HTML of a selection is
Code: Select all
// get HTML from selection
function getSelectionHTML(selection)
{
   var range = (document.all ? selection.createRange() : selection.getRangeAt(selection.rangeCount - 1).cloneRange());

   if (document.all)
   {
      return range.htmlText;
   }
   else
   {
      var clonedSelection = range.cloneContents();
      var div = document.createElement('div');
      div.appendChild(clonedSelection);
      return div.innerHTML;
   }
}


Call the function like this
Code: Select all
function FCKeditor_OnComplete( editorInstance )
{
   var selection = (editorInstance.EditorWindow.getSelection ? editorInstance.EditorWindow.getSelection() : editorInstance.EditorDocument.selection);
   alert(getSelectionHTML(selection)) ;
}
Freelance webdeveloper, FCKeditor plugin developer. FCKeditor contributor.

FCKPlugins: http://www.saulmade.nl/FCKeditor/FCKPlugins.php
FCKSkins: http://www.saulmade.nl/FCKeditor/FCKSkins.php
FCKSnippets: http://www.saulmade.nl/FCKeditor/FCKSnippets.php
saul11
 
Posts: 633
Joined: Mon Jun 19, 2006 5:39 am
Location: NL

Re: get selected HTML Source Code

Postby saul11 » Sat Nov 22, 2008 2:43 pm

Oops, there was a bug in the getSelectionHTML function. I've corrected in the post here above.
Freelance webdeveloper, FCKeditor plugin developer. FCKeditor contributor.

FCKPlugins: http://www.saulmade.nl/FCKeditor/FCKPlugins.php
FCKSkins: http://www.saulmade.nl/FCKeditor/FCKSkins.php
FCKSnippets: http://www.saulmade.nl/FCKeditor/FCKSnippets.php
saul11
 
Posts: 633
Joined: Mon Jun 19, 2006 5:39 am
Location: NL


Return to Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests