[ACCEPTED]-accessing a form that is in an iframe-iframe

Accepted answer
Score: 15
var ifr = document.getElementById( yourIframeId );
var ifrDoc = ifr.contentDocument || ifr.contentWindow.document;
var theForm = ifrDoc.getElementById( yourFormId );

Or you could have some code in the frame 2 that sets a variable in parent to the form, but 1 I wouldn't trust that method.

Score: 3

If your iframe has a name attribute, that can 2 be used as a window name. If the frame name 1 is "myframe":

myframe.document.getElementById("myform") // gives you the form element
Score: 1

Just made bookmarklet based on my older 2 post Is it possible to save form data to a data file on the local computer and then reload that text file back into a form to select those same items?

It is reads inside iframe and set result 1 JSON to textarea and prints to console:

javascript:o=document.getElementById("wf_IFid").contentDocument;
f=o.getElementsByTagName("input"), longest=f.length;
frm=f;
values={};
p=0;
for(a=0;a<longest;a++){
  el=frm[a];
  switch(el.type){
    case "checkbox":
      values[el.name]=el.checked;
      break;
    case "radio":
      if(el.checked)values[el.name]=el.value;
      else if(values[el.name]===undefined)values[el.name]=false;
      break;
    case "select-one":
      values[el.name]=el.selectedIndex<0?-1:el.options[el.selectedIndex].value;
      break;
    case "select-multiple":
      values[el.name]=[];
      for(i=0;i<el.options.length;i++){
        if(el.options[i].selected)values[el.name].push(el.options[i].value)
      }
      break;
    case "fieldset":
      break;
    case "button":
      break;
    case "submit":
      break;
    case "reset":
      break;
    case "file":
      break;
    case undefined:
      break;
    default:
      {
        if(el.getAttribute("aria-label")&&el.value){
          key=p++ + "|" + el.getAttribute("aria-label");
          values[key]=el.value
        }
      }
  }
}
t=o.createElement("textarea");
t.value=JSON.stringify(values, null, 2);
o.body.prepend(t);
console.log(JSON.stringify(values, null, 2));

More Related questions