0

I have an ordinary table in html in an iframe and I would like to access it from the parent page (yes they are on the domain and server) I've tried every kind of:

$("#Iframe").contents().find("#mytable td");

I don't have the impression it detects my table cell or table at all. I would like indeed to do an event on it.

       $("#mytable td")         
        .mousedown(function () 
        {
            isMouseDown = true;
            $(this).toggleClass("highlighted");
            isHighlighted = $(this).hasClass("highlighted");
            return false;
        })
        .mouseover(function () {
          if (isMouseDown) {
            $(this).toggleClass("highlighted", isHighlighted);
          }
        })

This event works perfectly on a single page with the css style linked but how should I do it when using an iframe? What's the correct sentence to detect my table in the iframe?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • `parent.opener.document.getElementById("street1").value = street1;` you can set the parent element from iframe like this – dreamweiver May 09 '13 at 14:03
  • Lots of suggestions [here](http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe?rq=1) – DOK May 09 '13 at 14:21

1 Answers1

0

make sure the selector, #Iframe, is actually on your iframe element as an ID.

Then you could setup something like this in your javascript...

var mframe = $('#Iframe').contents();
var mtable = mframe.find('#mytable td');

Then go to town with mtable, all within a $(window).load(function(){...}

klewis
  • 7,459
  • 15
  • 58
  • 102
  • 1
    Is using $(window).load will make sure that the events are bind after an iframe is loaded ? The iframe load and the document load is 2 different things. And note that it won't work if the iframe is from external domain. – Thanh Trung May 09 '13 at 14:28
  • Do you have direct control over your iframe content (i.e is it coming from your own server?) – klewis May 13 '13 at 13:28
  • Thanks for the answers but it still doesn't work properly. I can trigger an alert when my iFrame is ready, but not when my table is ready. The annoying thing in the website I'm working on, it's the fact that my table is launched when a drop down list is clicked. Otherwise there is no table. But sometimes I can trigger an alert when my table is ready but the table is not displayed. Any idea how to solve this problem? – user2366493 May 13 '13 at 13:38
  • Yeah I have control over the iframe content and like I said, it's on the same server and domain. But for some unknown reason, no css or javascript work at all directly in this iframe. It works from the parent page though. Is there something that could restrict the use of css and javascript in my iframe? I can't think of something like that. Of course if I can just put the javascript I want, it would be better to do it from the iframe directly. It's only a php page without anything else. It only "echoes" some html to create the table. – user2366493 May 13 '13 at 13:40
  • View the source code within the iframe, when the iframe is fully loaded, and then copy and paste it into a jsfiddle page so that it can be examined. From that point we should be able to find why you can't apply Jquery directly. – klewis May 13 '13 at 13:53
  • Ah great thinking. I've managed to make clickable rows on the iframe directly when launched alone. Now I need to find out why the parent page disables my javascript, jquery or css in the iframe when it launches it. It's launched with all kind of getxmlhttprequest and xmlreadystate... Don't ask me why, I don't know, I didn't code that. Thanks a lot blachawk. If you guys still have ideas about that, feel free to tell me. Thanks. – user2366493 May 15 '13 at 16:33