Answer: How to auto generate new list options...

N

Nick

In case anyone in is need...

<html>
<head>
<script language="javascript">
function getStates () {
stateCodes = new Array('AK','AL','AR','CA','CO','CT','DC','ETC');
for (var stateOptions='',$i=0; $i < stateCodes.length; $i++) {
stateOptions = stateOptions + "<option value=\"" + stateCodes[$i] +
"\">" + stateCodes[$i] + "</option>";
}
return stateOptions;
}
function addStateOption () {
var stateCount =
parseInt(document.getElementById('stateCounter').value) + 1;
for (emptyField=false, i=1; i < stateCount; i++) {
if (document.getElementById('s' + i).value == '') {
emptyField = true;
document.getElementById('s' + i).style.backgroundColor = "#FFFFD9";
} else {
document.getElementById('s' + i).style.backgroundColor = "#FFFFFF";
}
}
if (emptyField != true) {
document.getElementById('states').innerHTML =
document.getElementById('states').innerHTML
+ "<select id='s" + stateCount + "' name='s" + stateCount + "'>"
+ "<option value=''>Select State</option>"
+ getStates()
+ "</select>"
+ "<br>";
document.getElementById('stateCounter').value = stateCount;
}
}
</script>
</head>
<body>
<div id="states"></div>
<a href="javascript:addStateOption();">Add State</a>
<input id="stateCounter" type="hidden" value="0">
</body>
</html>
 
R

RobG

Nick said:
In case anyone in is need...
[...]

But first try:

<script type="text/javascript">

function addStateOptions(f){

if ( !document.createElement
|| typeof Option == 'undefined' ){
return;
}

var stateCodes = ['AK','AL','AR','CA','CO','CT','DC','ETC'];
var sel = document.createElement('select');
var sc;
for (var i=0, len=stateCodes.length; i<len; ++i){
sc = stateCodes
sel.options[sel.options.length] = new Option(sc, sc);
}
f.appendChild(sel);
f.stateCounter.value = i;
}

</script>

<div id="states"></div>
<form action="">
<input id="stateCounter" type="hidden" value="0">
<input type="button" value="Add states"
onclick="addStateOptions(this.form)"</form>

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top