views:

105

answers:

1

I have a form loading on a page that loads with an onclick event on the parent page using the following javascript. It gets loaded in a DIV.

function loadContent(elementSelector, sourceURL) {
 $(""+elementSelector+"").load("http://myurl.co.nz/"+sourceURL+"");
}

Then on another on click event (within the new form) one of the fields in the form is populated with a random password.

<label>Password:</label><input type="text" name="password" /><br/>
<input type="button" value="Generate Password" onClick="genPwd()"/> 

function genPwd(){
$.post("rpc.php", {
 method: "genPwd"
  },
 function(data,textstatus){
 document.form.password.value = data.message; 
 }, "json");
}

All the javascript on the page is included at load time.

Chrome and FF are fine with this but IE8 says that document.form.password does not exist, I assume because it is not in the original page. Any way around this?

Cheers

+3  A: 

AFAIK document.form is deprecated. You could use this cross browser solution instead:

$('#passwordFieldId').val(data.message);
Darin Dimitrov
you can likely still get away with document.forms[idxOrName].elements[passwordFieldId].value = ...;
scunliffe
yes, probably document.forms[index] could work but provided you are already using jQuery it would be better to use its full power.
Darin Dimitrov