locked
iframe double click get selected text RRS feed

  • Question

  • User181930479 posted

    I am using mark.js to highlight text , now I am trying to get the selected text from iframe by double clicking on the text that is highlighted so that i can send it to database using ajax .

    My below code will perform this operation , but by selecting the text and clicking on a button , what i need it by simply double clicking the highlighted text to trigger the button function. 

    below is my code

     <script>
    
           function naffy() {
               t1.text = "";
               var iframe = document.getElementById('if1');
               var idoc = iframe.contentDocument || iframe.contentWindow.document; // ie compatibility
               var text = idoc.getSelection().toString();
               if (text === "") {
    
                
               }
               else {
             
                   t1.value = text;
                   $('#t1').focus();
                   search1(); 
               }
    
           }
    
    
       </script>
    
    
    in the body section
    
      <input type="button" id="ora123" value="get meaning" onclick="naffy()" />

    Tuesday, January 23, 2018 8:14 AM

All replies

  • User-1838255255 posted

    Hi NAF,

    According to your description, as far as i know, you could use dbclick event to get selected value, please check the following code:

    Sample Code:

    <head runat="server">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
            $(function () {
                $("p").dblclick(function () {
                    var text = getSelectedText();
                    alert(text);
                    naffy();
                })
            })
    
            function getSelectedText() {
                if (window.getSelection) {
                    return window.getSelection();
                }
                else if (document.selection) {
                    return document.selection.createRange().text;
                }
                return '';
            }
    
            function naffy() {
                t1.text = "";
                var iframe = document.getElementById('if1');
                var idoc = iframe.contentDocument || iframe.contentWindow.document; // ie compatibility
                var text = idoc.getSelection().toString();
                if (text === "") {
    
                }
                else {
                    t1.value = text;
                    $('#t1').focus();
                    search1();
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <p>
                    Hello, world!
                </p>
            </div>
        </form>
    </body>

    Best Regards,

    Eric Du 

    Wednesday, January 24, 2018 6:39 AM