Anyway to get the the selected text with Javascript

Last post 09-15-2005, 3:39 PM by Adam. 3 replies.
Sort Posts: Previous Next
  •  09-15-2005, 1:39 PM 10632

    Anyway to get the the selected text with Javascript

    I am trying to have a user enter an ISBN and then select it and then I do some remote scripting so I can popluate the editor with book data for the isbn.

    This line of javascript is failing in both IE and Netscape/Firefox

          if (e.setSelectionRange) {    
             strText = e.value.substring(e.selectionStart, e.selectionEnd);
          }
          else {
             strText = document.selection.createRange().text;

           }

    This works with a simple textarea html forms control

    Thanks
  •  09-15-2005, 1:58 PM 10634 in reply to 10632

    Re: Anyway to get the the selected text with Javascript

    Please use the following JavaScript API to write your own script:
     
        
        // get the cute editor instance
       var editor1 = document.getElementById('CE_Editor1_ID');
       
       //Get the editor content 
       var editdoc=editor1.GetDocument();
     
       //Get the editor content 
       var editdoc=editor1.GetDocument();
     
       //Get the editor selection 
       var editselection=editor1.GetSelection();
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  09-15-2005, 2:19 PM 10638 in reply to 10634

    Re: Anyway to get the the selected text with Javascript

    Thanks perfect, got the selection.  One more question: How do I write to the editor with with my data right after the selection.
  •  09-15-2005, 3:39 PM 10645 in reply to 10638

    Re: Anyway to get the the selected text with Javascript

    Here are the client side APIs:

    function getHTML()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        // Get the editor HTML
        document.getElementById("myTextArea").value = editor1.getHTML();
       }  
       
       function setHTML()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        // Set the editor
        editor1.setHTML(document.getElementById("myTextArea").value);
       }
       
       
       function PasteHTML(html)
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        editor1.PasteHTML(html);
       }
       
       function SetActiveTab(tab)
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        editor1.SetActiveTab(tab);
       }
       
       
       function setFocus()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        editor1.FocusDocument();
       }
       
       
       function ExecCommand()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        var CommandObj = document.getElementById('Commands');
        var cmd = CommandObj.options[CommandObj.selectedIndex].text;
        var val = CommandObj.options[CommandObj.selectedIndex].value;
        editor1.ExecCommand(cmd,false,val);
       }
       
       function CE_attachEvent()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // attach Event
        editdoc.attachEvent('onkeypress',HandleChange);
       } 
       
       
       function HandleChange()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        alert(editdoc.body.innerHTML);
       }  
        
       
       function CE_detachEvent()
       {
        // get the cute editor instance
        var editor1 = document.getElementById('CE_Editor1_ID');
        
        //Get the editor content 
        var editdoc=editor1.GetDocument();
        // detach Event
        editdoc.detachEvent('onkeypress',HandleChange);
       }

    To answer your question, you can just use the editor1.PasteHTML(html) method.

     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML