0

I am using the following code for Highlighting the text in the Iframe but i cant get it to work

  function getSelectedText() {
        if (window.getSelection) {;
            return window.getSelection().toString();
        } else if (document.getSelection) {;
            return document.getSelection();
        } else if (document.selection) {;

            return document.selection.createRange().text;
        }
    }
    $(document).ready(function () {


        $("#iframe1").live("mouseup", function () {
            selection = getSelectedText();
            if (selection.length >= 3) {

                $(this).html($(this).html().replace(selection, "<span class='highlight'>" + selection + "</span>"));

            }
        });
    });
    });
Yahoo
  • 4,093
  • 17
  • 59
  • 85

2 Answers2

1

If iframe1 is the ID of the <iframe>, you need to place it in quotation marks for your selector.

So instead of:

$(#iframe1).live("mouseup", function () {
   //...

you need:

$('#iframe1').live("mouseup", function () {
   //...
user113716
  • 318,772
  • 63
  • 451
  • 440
0
$( function() {
   var myiframe = $($.browser.msie ? frames["iframe1"] : "#iframe1");
   // doesn't work in Opera < 9
   myiframe.load( function() {
       selection = getSelectedText();

       //below code works but i dont know about $(this) will work or not but you can try your code
       //var w = this.contentWindow;
       //if(!w) w = myiframe[0]; // IE
       //alert("Number of anchors: " + w.$("a").size());
       //alert("Document title: " + w.document.title);

        if (selection.length >= 3) {
            $(this).html($(this).html().replace(selection, "<span class='highlight'>" + selection + "</span>"));
        }
   })
})
Wazy
  • 8,822
  • 10
  • 53
  • 98
  • It didnt work , or i think i didnt know how to implement it . Ijust copied w hole of your code and pasted it into $(#iframe1).live("mouseup", function () { is it the right way? – Yahoo Jan 14 '11 at 18:38