473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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').v alue) + 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>

Nov 23 '05 #1
3 1559
Nick wrote:
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[i]
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>

[...]
--
Rob
Nov 23 '05 #2
Nick wrote:
In case anyone in is need...


Prepare to be roasted :)

Check the FAQ at http://www.jibbering.com/faq/ to see that some parts of
your code are not recommended practices.
Also, there are better ways to solve the problem you appear to be trying to
solve.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 23 '05 #3
Thank Matt, ill try guessing which better ways to solve the problem.
-Nick

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Prowler | last post by:
In our current application we have a page whose sole purpose for existence is to permit the user to select from a list (subsequent to our login page). We would like to have the list drop down...
5
by: Brakeshoe | last post by:
I would like to find out how to turn on the Auto Complete variable names feature while editing source code in Visual Studio. It seems to appear on intermittantly, and when it does the variable...
8
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they...
0
by: Waran | last post by:
I need to create a Auto suggests Textboox like in http://www.google.com/webhp?complete=1&hl=en I have completed this using AJAX.NET for Framework 1.1 . I have some design issues after the data is...
8
by: dougawells | last post by:
Hi - I'm hoping for help with the auto-generation of a hyperlinked listing of all files in a directory. The server I use does not auto-generate this. So, when someone comes to this directory and...
0
by: Tom | last post by:
I'm using the DB_DataObject script createTables.php to auto-generate the necessary database schema on two databases. Using the .ini approach (not the in-line PHP approach) to configure...
1
by: gauravtechie | last post by:
Hi all, I have a javascript that is reloading itself on every menu change, Now the problem is that the values of the product doesn`t reset itself on catalogue change, the code is <html>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.