Alternative to window.frameelement.document.forms[0]?

589 Views Asked by At

I am looking at some code that is failing in current versions of Chrome and IE 11. It errors because of the line:

var form = window.frameElement.document.forms[0];

With error:

"JavaScript runtime error: Unable to get property 'forms' of undefined or null reference"

Window and frameElement exist but the document is null.

The context of the above is a popup window take a zip code, displays some results and a "select" button. On click of the button calls a method which calls the above line to then assign the selected location into some values.

returnValues(id, name){
  var form = window.frameElement.document.forms[0];
  form.ctl00$MainContentPlaceHolder$uxResControl$uxRenLoc$RenLocCodeField.value = id;
  form.ctl00$MainContentPlaceHolder$uxResControl$uxRenLoc$RenLocNameField.value = name;
  ...
}

With these nested user controls I am a bit confused as to how to "drill" down and assign the selected value to the hidden value RenLocCodeField or RenLocNanmeField.

1

There are 1 best solutions below

4
Abhinav On

Using usual ASP.NET Javascript query is always a pain!

instead of the following line:

form.ctl00$MainContentPlaceHolder$uxResControl$uxRenLoc$RenLocCodeField.value = id;
form.ctl00$MainContentPlaceHolder$uxResControl$uxRenLoc$RenLocNameField.value = name;

you can use the following lines in javascript method, assuming RenLocCodeField and RenLocNameField are the IDs generated for the respective fields.

document.getElementById("RenLocCodeField").value = id;
document.getElementById("RenLocNameField").value = name;