-4

Can someone suggest an alternative to the following code for passing hidden form field values? Thanks in advance for suggestions

//Working Fine but breaks some forms
document.write("<INPUT TYPE=\"hidden\" id=\"Field1\" name=\"Field1\" VALUE=\"" + VarXYZ + "\">\r\n");
user1505467
  • 1
  • 1
  • 2

1 Answers1

1

bad aproach. at the first, you should not touch the elements until finish loading all of DOM elements.

if DOM elements are loaded, you do like following.

document.getElementById('Field1').value = VarXYZ;
document.getElementById('Field2').value = VarABC;

About way to handle document loaded, there is difference in some browsers. So you've better to use jQuery or some useful libraries. Most easy way is:

window.onload = function() {
    document.getElementById('Field1').value = VarXYZ;
    document.getElementById('Field2').value = VarABC;
}
mattn
  • 7,571
  • 30
  • 54