473,395 Members | 2,783 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,395 software developers and data experts.

Help modifying select menu dynamically!

I have a fixed html structure, where only one form and a simple select menu
will exist on an html page, as follows:

<form action="order" method="POST">
<select name="dinner">

<option value="1">Pizza</option>
<option value="2">Hot Vindaloo</option>
<option value="3">Fish-n-Chips</option>
<option value="4">Currywurst</option>

</select>
<input type="submit" value="Place order">
</form>

I'd like the initial option display not say "Pizza" but instead
display "Please select your order", without changing the above html code,
in other words, generated by Javascript.

The way I thought this could be done is:

1) When the html document loads, body onload, add an option to the list on
the top of the options, so it will have five options instead of four (but
without knowing the number in advance). Exactly how can I do this in
Javascript?
(Something like: document.forms[1] = new Option('text','value') ...???)

2) As I make the first onChange toggle action, I would like to REMOVE the
initially created "Please select ..." option, so that options only contain
valid food choices and is the length of the original html structure again.

3) In case the user did not select anything, I would like to halt the
submission by the onSubmit() event handler, and pop up an alert('Please
select order') instead.

I'm not quite sure how to code the above however, or if it will work on most
current browsers? An initial search indicates that IE or/and Opera have
problems in modifying drop menus dynamically, but perhaps this information
is outdated and now relates mostly to extinct browsers?

I would greatly appreciate any comments, tips and especially code bits!
Dec 5 '06 #1
6 2142
ASM
tuxedo a écrit :
I have a fixed html structure, where only one form and a simple select menu
will exist on an html page, as follows:

<form action="order" method="POST">
<select name="dinner">

<option value="1">Pizza</option>
<option value="2">Hot Vindaloo</option>
<option value="3">Fish-n-Chips</option>
<option value="4">Currywurst</option>

</select>
<input type="submit" value="Place order">
</form>

I'd like the initial option display not say "Pizza" but instead
display "Please select your order", without changing the above html code,
in other words, generated by Javascript.
var D = document.forms['myForm'].dinner;
D.options[0].text = "Please select your order";
The way I thought this could be done is:

1) When the html document loads, body onload, add an option to the list on
the top of the options, so it will have five options instead of four (but
without knowing the number in advance). Exactly how can I do this in
Javascript?
(Something like: document.forms[1] = new Option('text','value') ...???)
function addFirst(where, newText, newVal) {
var o = new Option(newText, newVal);
where.options = where.options.unshift(o);
}

<body onload="addFirst(
document.forms['myForm'].dinner,
'Please select your order',
'');"
2) As I make the first onChange toggle action, I would like to REMOVE the
initially created "Please select ..." option, so that options only contain
valid food choices and is the length of the original html structure again.
document.forms['myForm'].dinner.options.shift();
3) In case the user did not select anything, I would like to halt the
submission by the onSubmit() event handler, and pop up an alert('Please
select order') instead.
<form blah
onsubmit="var D = document.forms['myForm'].dinner.options;
var ok = D[0].text!='Please select your order';
if(!ok) alert('go to choice a pizza !');
return ok" >
Don't forget to give 'myForm' as name for your form !
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 5 '06 #2
ASM wrote:

[...]
Don't forget to give 'myForm' as name for your form !
Merci - code looks good! I will test.
Dec 5 '06 #3
ASM
tuxedo a écrit :
ASM wrote:

[...]

Merci - code looks good! I will test.
I tried nothing of this code nor tested it

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 5 '06 #4
ASM wrote:

[...]
I tried nothing of this code nor tested it
I now tried it but could unfortunately not make this work.

Firstly, I've tried simply to add an option on top of the select menu with
the "Please select your order" text, using the addFirst() function, as in
my below example, but without passing the parameters within the onload
function parameters, as follows:
<script ... >

function addFirst(){
var where = document.forms['myForm'].dinner;
var o = new Option('Please select order','');
where = where.options.unshift(o);
}

</script>
<body onload="addFirst()">

<form name="myForm" action="order" method="POST">

<select name="dinner">
<option value="1">Pizza</option>
<option value="2">Hot Vindaloo</option>
<option value="3">Fish-n-Chips</option>
<option value="4">Currywurst</option>
</select>

<input type="submit" value="Place order">

</form>
According to the earlier post, should teh above not add an option on top of
the select menu containing the "Please select order" text?

The Javascript error when running it is "where.unshift is not a function",
which I'm not sure what it means....

How exactly should I add an option on top of the select menu containing the
new text string?

Thanks for any tips!

Dec 6 '06 #5
ASM
tuxedo a écrit :
ASM wrote:

[...]
>I tried nothing of this code nor tested it

I now tried it but could unfortunately not make this work.
unshift(x y z) works fine with an Array
options are probably not seen as an array ?

this here works, but there is certainly something more elegant :

function addFirst(){
var where = document.forms['myForm'].dinner;
where.length++;
for(var i=where.length-1; i>0; i--)
{
var a = -1+i;
where.options[i].text = where.options[a].text;
where.options[i].value = where.options[a].value;
}
var o = new Option('Please select order','');
where.options[0] = o;
}

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 6 '06 #6
ASM wrote:

[...]
unshift(x y z) works fine with an Array
options are probably not seen as an array ?
Yes I read now that options are objects, contained by the select object.
this here works, but there is certainly something more elegant :
It certainly works and who cares what it looks like!? Adding the option was
obviously the hard part. The rest should be more straightforward.

Many thanks!
Dec 7 '06 #7

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

Similar topics

4
by: Nanos | last post by:
For the form I need to create drop down select with say five different options. There also should be possibility to enter own option if one is not listed. How can I do it.? Thank you in advance...
0
by: work4u | last post by:
My case is from the client site web page, there is a select menu, on a form, which dynamically show all data from a database But the data under this select menu which need to follow a rule which...
20
by: titi | last post by:
Question The road and traffic authority for a small country currently uses a system to store information about all 'currently' licensed drivers. A licensed driver has the following info stored...
3
by: NewmanBT | last post by:
As you can see from the code below, several textboxes will be dynamically created and each will be tied to an org. The ChangeComment function should allow for an update to the database whenever...
2
by: devendra pardeshi | last post by:
hi friends/seniors i am stuck on one problem in VB 6.0 and need solution. see if u can help me. first i describe the problem. Can u imagin the WinZip scenario. we right click on some file...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
4
by: Bob Homes | last post by:
In VB6, I used a system, which I loved, whereby I assigned a "helpId" to each menu item; that way, you could rest the cursor on the item (without actually running it) and then press F1 to get...
4
windows_mss
by: windows_mss | last post by:
When I Select Source & Destination Dynamically, Path Getting Scatter Across The Map... hi, i can able to get the Correct Route and Path for the corresponding Source and destination, like...
3
by: =?Utf-8?B?ZWFndWlsYXI=?= | last post by:
Hi, I am trying to dynamically generate a menu, based on entries on a text or xml file. The text file contains the "tree" after which the menu will need to be created. Something like the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.