JQuery and event binding to iframe

** I am currently using the jquery textselect plugin to trigger alerts based on selection text anywhere on the page and it works fine by doing something like:

$(document).ready(function() {
    $(document).bind('textselect', function(e) {
        alert(e.text); 
    });
});

Since then, I had to add an iframe to the page, and I need a choice of text to work on the text inside the iframe. I am trying to do something like this, but it does not work:

$(document).ready(function() {
    $('#iframeId').load(function() {
    $(document.getElementById("iframeId").contentWindow).bind('textselect',function(e) {
    alert(e.text); 
    });
});

At this point, I tried a whole mess for links to an iframe document without any success. Any ideas? **

+2
source share
2 answers

Access to it can be obtained as follows:

$(document).ready(function() {
  $('#iframeId').load(function() {
    alert("Loaded");
    $('#iframeId').contents().find("body").bind('textselect', function(e) {
      alert(e.text);
    });
  });
});

. , iframe - , .

+5

... , , . . iframe - :

window.frames["ifFrameName"].document
0

Source: https://habr.com/ru/post/1568113/


All Articles