doug777
10-28-2004, 06:20 AM
Can anyone help?
I am trying to create a select element and populate it with the same options that are in the select element in the html code.
The user may click the 'More' button to add the line shown in the html as many times as required. The name of the elements is updated each time by adding the number in the global variable rmblocks. The line is added to the displayed page correctly and the input elements pass the correct data forward, but inspecting the Form array on the subsequent page shows that the select elements BlkSeason1 etc from the javascript are not present. The html select element is correct and contains the options shown.
On the displayed page, the select elements from the javascript appear but have no options, presumably because the select element has not been created correctly or not added to the form correctly, even though it has been added to the document correctly.
I can't see what's wrong, can anyone help?
Doug - code follows:-
<body>
<script language="JavaScript" type="text/javascript">
<!--
var rmblocks = 0;
function addRmBlocks() {
var elem1, elem2, elem3, elem4, txt1, txt2, txt3, txt4, elem2name, elem3name, elem4name, oplen;
rmblocks = rmblocks + 1;
elem2 = document.createElement("input");
elem2name = "MaxRms" + rmblocks;
elem2.setAttribute("name", elem2name);
elem2.setAttribute("type", "text");
elem2.setAttribute("value", "0");
elem2.setAttribute("size", "4");
elem2.setAttribute("maxlength", "3");
addcontract.appendChild(elem2);
elem3 = document.createElement("input");
elem3name = "BlkMinusDays" + rmblocks;
elem3.setAttribute("name", elem3name);
elem3.setAttribute("type", "text");
elem3.setAttribute("value", "0");
elem3.setAttribute("size", "4");
elem3.setAttribute("maxlength", "2");
addcontract.appendChild(elem3);
elem4 = document.createElement("select");
elem4name = "BlkSeason" + rmblocks;
elem4.setAttribute("name", elem4name);
elem4.setAttribute("id", elem4name);
elem4.setAttribute("size", 16);
addcontract.appendChild(elem4);
elem1 = document.createElement("p");
txt1 = document.createTextNode("Up to ");
txt2 = document.createTextNode(" rooms may be blocked for up to ");
txt3 = document.createTextNode(" days before check in during ");
txt4 = document.createTextNode(" season.");
elem1.appendChild(txt1);
elem1.appendChild(elem2);
elem1.appendChild(txt2);
elem1.appendChild(elem3);
elem1.appendChild(txt3);
elem1.appendChild(elem4);
elem1.appendChild(txt4);
document.addcontract.insertBefore(elem1, be);
oplen = document.addcontract.BlkSeason;
elem4name = "document.addcontract." + elem4name;
for (var i = 0; i < oplen.options.length; i++) {
elem4name.options[i] = new Option(document.addcontract.BlkSeason.options[i].text, document.addcontract.BlkSeason.options[i].value);
}
}
//-->
</script>
<form name="addcontract" method="post" action="test_processor.cfm">
<p>Up to
<input name="MaxRms" type="text" id="MaxRms" value="10" size="4" maxlength="3" />
rooms may be blocked for up to
<input name="BlkMinusDays" type="text" id="BlkMinusDays" value="0" size="4" maxlength="2" />
days before check in during
<select name="BlkSeason" id="BlkSeason">
<option value="Regular">Regular</option>
<option value="High">High</option>
<option value="Peak">Peak</option>
</select>season.</p>
<p id="be" align="center"><input name="blockage" type="button" id="blockage" value="More" onclick="addRmBlocks()" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>
</body>
I am trying to create a select element and populate it with the same options that are in the select element in the html code.
The user may click the 'More' button to add the line shown in the html as many times as required. The name of the elements is updated each time by adding the number in the global variable rmblocks. The line is added to the displayed page correctly and the input elements pass the correct data forward, but inspecting the Form array on the subsequent page shows that the select elements BlkSeason1 etc from the javascript are not present. The html select element is correct and contains the options shown.
On the displayed page, the select elements from the javascript appear but have no options, presumably because the select element has not been created correctly or not added to the form correctly, even though it has been added to the document correctly.
I can't see what's wrong, can anyone help?
Doug - code follows:-
<body>
<script language="JavaScript" type="text/javascript">
<!--
var rmblocks = 0;
function addRmBlocks() {
var elem1, elem2, elem3, elem4, txt1, txt2, txt3, txt4, elem2name, elem3name, elem4name, oplen;
rmblocks = rmblocks + 1;
elem2 = document.createElement("input");
elem2name = "MaxRms" + rmblocks;
elem2.setAttribute("name", elem2name);
elem2.setAttribute("type", "text");
elem2.setAttribute("value", "0");
elem2.setAttribute("size", "4");
elem2.setAttribute("maxlength", "3");
addcontract.appendChild(elem2);
elem3 = document.createElement("input");
elem3name = "BlkMinusDays" + rmblocks;
elem3.setAttribute("name", elem3name);
elem3.setAttribute("type", "text");
elem3.setAttribute("value", "0");
elem3.setAttribute("size", "4");
elem3.setAttribute("maxlength", "2");
addcontract.appendChild(elem3);
elem4 = document.createElement("select");
elem4name = "BlkSeason" + rmblocks;
elem4.setAttribute("name", elem4name);
elem4.setAttribute("id", elem4name);
elem4.setAttribute("size", 16);
addcontract.appendChild(elem4);
elem1 = document.createElement("p");
txt1 = document.createTextNode("Up to ");
txt2 = document.createTextNode(" rooms may be blocked for up to ");
txt3 = document.createTextNode(" days before check in during ");
txt4 = document.createTextNode(" season.");
elem1.appendChild(txt1);
elem1.appendChild(elem2);
elem1.appendChild(txt2);
elem1.appendChild(elem3);
elem1.appendChild(txt3);
elem1.appendChild(elem4);
elem1.appendChild(txt4);
document.addcontract.insertBefore(elem1, be);
oplen = document.addcontract.BlkSeason;
elem4name = "document.addcontract." + elem4name;
for (var i = 0; i < oplen.options.length; i++) {
elem4name.options[i] = new Option(document.addcontract.BlkSeason.options[i].text, document.addcontract.BlkSeason.options[i].value);
}
}
//-->
</script>
<form name="addcontract" method="post" action="test_processor.cfm">
<p>Up to
<input name="MaxRms" type="text" id="MaxRms" value="10" size="4" maxlength="3" />
rooms may be blocked for up to
<input name="BlkMinusDays" type="text" id="BlkMinusDays" value="0" size="4" maxlength="2" />
days before check in during
<select name="BlkSeason" id="BlkSeason">
<option value="Regular">Regular</option>
<option value="High">High</option>
<option value="Peak">Peak</option>
</select>season.</p>
<p id="be" align="center"><input name="blockage" type="button" id="blockage" value="More" onclick="addRmBlocks()" /></p>
<p><input type="submit" name="Submit" value="Submit" /></p>
</form>
</body>