0

Just like the question says, I'm trying to clear a form from a modal window while the modal stays up. I've tried:

if (myDocument.title == "Modal Window") {
    parent.document.getElementById("textbox")
}

(I need it to do more than 1 tb, but used that just to try to get there. No luck.

It is contained within an iFrame, so I tried:

if (myDocument.title == "Modal Window") {
    var ifr = document.getElementById("iFrame")
    var form = ifr.document.getElementById("form")
    ClearForm(form)
}

The ClearForm(form) function I stole from another Stack Overflow answer:

function ClearForm(form) {
    $(':input', form).each(function () {
        var type = this.type;
        var id = this.id;
        if (type == 'text' && id != 'text2')
            this.value = "";
    });
}

That 'text2' is one specific tb that we need to remain populated.

Any idea what I'm missing? I've been plagued with this bug for weeks.

C T
  • 1
  • What error are you getting? See [this answer](http://stackoverflow.com/questions/3999101/html-getting-document-from-iframe) for how to access elements in an iframe (assuming it's loading the document from the same origin). – linstantnoodles Feb 19 '14 at 17:08
  • That's the problem - I'm not getting any errors, it's just not working. [Arguably more frustrating than getting a js error.] – C T Feb 19 '14 at 17:09
  • Well do you know where the code is breaking? (do a search on how to debug JavaScript). If that doesn't work try putting the code in fiddle because I think we need more information. – linstantnoodles Feb 19 '14 at 17:13
  • I'm going to do some more research now and try to update in a few minutes. I'm very new to js. – C T Feb 19 '14 at 17:19

1 Answers1

0

I expect your issue is that the form is within an iFrame - most browsers won't allow you to modify elements within an iFrame, from the parent page, if they aren't from the same origin (or if the server is set up to deny it, or if you're looking at the page locally... see here for more details)

To double-check, try moving the form markup into the same page as the modal is in and run your function ClearFormfrom there. I expect that you'll then find it works.

Your only way around this would be to include the ClearForm function within the iFrame'd page, and then trigger it from the parent.

Community
  • 1
  • 1
johnkavanagh
  • 4,614
  • 2
  • 25
  • 37
  • "most browsers won't allow you to modify elements within an iFrame, from the parent page." Can you elaborate? With the exception of the same origin policy, I don't think that's true. – linstantnoodles Feb 19 '14 at 17:09
  • Sure. Have a read [here](http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe). In order to do as is being asked, the `ClearForm` function would need to reside within the iFrame'd page (rather than the parent). – johnkavanagh Feb 19 '14 at 17:14
  • They both use the same js, but have different htm's if that helps at all. – C T Feb 19 '14 at 17:20
  • @johnkavanagh The same origin policy prevents the script from parent document A from accessing document B loaded from a different origin - not if the script in the parent document doesn't exist in document B. However, that seems to be what you're suggesting, which I don't think is correct. Am I misunderstanding anything here? – linstantnoodles Feb 19 '14 at 17:29