1

I have HTML form with a textarea and a button which calls the function below when a part of the text is selected. With de id of the clicked button and the selected text I want to call a php script by the POST method.

$(document).ready(function() {
    $(".postbit_buttons").on("click", ".quoted_clicked", function() {
        var post = this.id;
        var selected = GetSelectedText();
        alert(" post = " + post + " seleted text = " + selected);
        $.post("test_quoted.php", {
                pid: post,
                selection: selected
            })
            .done(function() {
                alert("done")
            });
    });
});

Function GetSelectedText() to get the selection was found here. The first alert is shown with the correct information. However, after clicking OK I get the following error message on the browser console:

TypeError: 'collapse' called on an object that does not implement interface Selection.

I have used a similar construct in another part of the forum software of which this is a part, and that works.

I have pain in my eyes of staring at this, but cannot find the cause. Is there anybody who can help me on this?

@Rory The code of function GetSelectedText() is:

function GetSelectedText()
{
  var selectedText=(
        window.getSelection
        ?
            window.getSelection()
        :
            document.getSelection
            ?
                document.getSelection()
            :
                document.selection.createRange().text
     );
 if(!selectedText || selectedText=="")
 {
    if(document.activeElement.selectionStart)
    {
     selectedText = document.activeElement.value.substring(
          document.activeElement.selectionStart
          . document.activeElement.selectionEnd);
    }
 }
 return selectedText;
}

Thanks to Rory's question I tried things out. I discovered that is was the variable "selected" in which the selected text is stored was the cause of the error. I now use the following version of GetSelectedText():

function GetSelectedText () {
    if (window.getSelection) {  // all browsers, except IE before version 9
        var range = window.getSelection ();
        return range.toString ();
    } 
    else {
        if (document.selection.createRange) { // Internet Explorer
            var range = document.selection.createRange ();
            return range.text;
        }
    }
}

and it works!! Sorry for bothering you. I do practice programming since 1968, but almost 100% was scientific computing with FORTRAN. Javascript is very new to me.

Regards, Ad Bakker

Ad Bakker
  • 19
  • 3
  • Can you please post the source of your `GetSelectedText()` function. – Rory McCrossan Jun 01 '15 at 09:48
  • 1
    Where is `GetSelectedText()` function? check this: http://jsfiddle.net/L9bvU/1/ or this http://stackoverflow.com/questions/14553534/how-to-get-selected-text-with-javascript – Radonirina Maminiaina Jun 01 '15 at 09:49
  • 2
    Somewhere you're calling `collapse` on an object that isn't a valid `Selection`, it doesn't seem to be in the posted code, so who knows where ? – adeneo Jun 01 '15 at 09:51
  • In your "HTML form with a textarea and a button" you have collapsible content. I assume that the plugin (or your code) cannot use the `collapse` function because the element is not properly selected (as a jQuery object). I do not believe atm that `GetSelectedText()` or the code you have above is affecting any of that, so it's better you provide the HTML and the js plugins/code you are using for collapsing areas so people can help you. – Armfoot Jun 01 '15 at 10:01
  • @ Radonirina Maminiaina. I only gave the relevant coding for the problem, the function GetSelectedText() works, because in the alert the selected text is shown. – Ad Bakker Jun 01 '15 at 11:03
  • @Armfoot. I will chech the HTML foor collapsed items. This is rather complicated, so this will take a while I fear. – Ad Bakker Jun 01 '15 at 11:07
  • function GetSelectedText() { var selectedText=( window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text ); if(!selectedText || selectedText=="") { if(document.activeElement.selectionStart) { selectedText = document.activeElement.value.substring( document.activeElement.selectionStart . document.activeElement.selectionEnd); } } return selectedText; } – Ad Bakker Jun 01 '15 at 11:47

0 Answers0