I've tried the following and I get an error message (syntax):
<form>
<input type="hidden" name="cond" id="cond">
<select onchange = "document.getElementById("cond").value ='blog';">
<option value = "Solomon">Solomon</option>
<option value = "Great Plains">Great Plains</option>
<option value = "Axapta">Axapta</option>
</select>
Any ideas?
:
Hi MikeR,
Let me share with you a little web programming secret that should help
you
to figure this sort of thing out for yourself in the future.
You can debug JavaScript! All you need is Internet Explorer and
FrontPage,
which comes with Microsoft Script Debugger.
First, you need to set up Internet Explorer, by going into
Tools|Intenet
Options. Click the "Advanced" tab. Find the check box that says
"Disable
Script Debugging (Internet Explorer)" and UNcheck it (it is checked by
default). From now on, any JavaScript error will cause IE to ask you if
you
want to debug the script in the Debugger. In the Debugger, you can step
through the code one instruction at a time, as well as looking at all
of the
HTML elements and JavaScript variables in the page, to examine their
state.
In addition, you can put BreakPoints into your JavaScript. A BreakPoint
is
an instruction that interrupts the execution and allows you to open the
script in the Debugger. Once you've learned to do these things, you'll
be a
JavaScript expert in no time at all.
I checked your code, by creating an HTML page in FrontPage, pasting
your
code into it (after fixing it up so that it was syntactically correct,
like
taking the ellipsis out of the <form> tag, and adding a </form> tag,
but not
changing anything else). Here is what I ran in my Debugger:
<html>
<head>
<base>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form>
<input type="hidden" name="cond">
<select onchange="debugger;this.form.cond.value = 'blog';">
<option value="sasdf">asdfsaf</option>
<option value="sassy">sassy</option>
<option value="drippy">drippy</option>
</select>
</form>
</body>
</html>
See the "debugger" statement in the select object? That's where it
broke
into the debugger. I then executed the next instruction and looked at
the
"cond" hidden field value using a QuickWatch. Lo and behold, it had the
value that I selected in it.
Now, you asked me if having more than one form field with the same name
would cause a problem. The answer is, most defintely YES. Of course,
now
you're disappointed that you won't be able to use the technique you've
been
working on. Take heart, my friend, all is not lost. A little knowledge
is a
dangerous thing, so learn all you can about JavaScript if you plan to
continue programming with it. Here's your answer:
When a form submits, it passes the "name" and "value" properties of its
form
elements in the Request. That is all. It passes no other properties of
its
form elements in the Request. Now, in an HTML document, HTML elements
all
have an "id" property. They don't all have a "name" property. Why, you
may
ask? Well, I'll tell you. The "id" property is strictly for use on the
client side by CSS and JavaScript.
Is the light coming on now?
Now, in JavaScript there is a method that returns ANY HTML element, by
passing its id to the method. To access any HTML element, you simply
assign
it an id, and pass that to the function. So, you can have multiple form
fields with the same name, but different ids. It is very important to
assign
unique ids to HTML elements for this reason, but not so important to
give
them unique names, and in fact, in a case like yours, perhaps a good
idea to
give them the same names. The method I'm talking about is
document.getElementById("id"). Here's an example for your problem:
<html>
<head>
<base>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form>
<input type="hidden" name="cond" id="cond">
<select onchange="document.getElementById("cond").value = 'blog';">
<option value="sasdf">asdfsaf</option>
<option value="sassy">sassy</option>
<option value="drippy">drippy</option>
</select>
</form>
</body>
</html>
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
Thank you. But still no joy. Even this doesn't set the value of the
hidden
field.
<form....>
<input type="hidden" name="cond">
<select onchange="this.form.cond.value = 'blog';"
<option value="sasdf">asdfsaf</option>
<option value="sassy">sassy</option>
<option value="drippy">drippy</option>
</select>
Also tried it with double quotes around blog.
This is all in loop that writes a table based on a database, so there
are
3 identical select/option dropdowns in the form. It that a DOH!! ?
cond does make it to the receiving page as an array (3 elements) of
request.form variables, but all are empty.
MikeR
Kevin Spencer wrote:
Hi MikeR,
"onchange" is an event handler for the "onchange" event that is
fired by
a select object when the user selects something other than what is
already showing in the sloect object (drop-down list box). You can
check
for it with an if statement. Example:
<select onchange="if (this.selectedIndex > 0) this.form.cond.value =
this.options[this.selectedIndex].value;" >