Pass formname and select name to function

M

Mark

Hi - I have a dynamically created table, which has a number of forms -
each named consecutively as 'adduserX' where X is a number generated
from ASP.

Within each form, I need to have a button which will call JavaScript,
but pass the name of the form the button is in, as well as a select list
name.

<input name="check" type="button" onclick="mt_showHilite(this.newname)"
value="submit">

The Javascript function needs to get the options from the select list:

function mt_showHilite(selname){
var opts=selname.options;

...but I keep getting an error 'options' is null or not an object.

I know the problem lies in my onclick text, but can't figure it out.

Can anyone please help?

Thanks,
 
L

Lasse Reichstein Nielsen

Mark said:
Within each form, I need to have a button which will call JavaScript,
but pass the name of the form the button is in, as well as a select list
name.

What is the select element's name? How do you find it? What is the
structure of the form? Tell us more!
<input name="check" type="button" onclick="mt_showHilite(this.newname)"
value="submit">

The input element doesn't have a property called "newname". You
pass the value "undefined" to the function.

If you really needed to pass the form *and* select element's names, you
could write
onclick="someFunction(this.form.name,this.form.elements[2].name)"
where "2" is the index of the select element in the form.
(If you know the name when buidling the page, just write that
directly, ofcourse).

More likely, you will want to pass the form and select elements
themselves as arguments. You can always find the name then. I.e.,
drop the "name" from the above.
The Javascript function needs to get the options from the select list:

function mt_showHilite(selname){
var opts=selname.options;

You expect "selname" to be the select element itself, not its name
(the name is a string and doesn't have an "options" property). Good
choice, though, I would stay with it (and change "selname" to "sel").
..but I keep getting an error 'options' is null or not an object.

Because it is undefined.
I know the problem lies in my onclick text, but can't figure it out.

I am not sure you know how to refer to form elements from Javascript.
Show us the form, and we can tell you what to write.
 
L

Lee

Mark said:
Hi - I have a dynamically created table, which has a number of forms -
each named consecutively as 'adduserX' where X is a number generated
from ASP.

Within each form, I need to have a button which will call JavaScript,
but pass the name of the form the button is in, as well as a select list
name.

<input name="check" type="button" onclick="mt_showHilite(this.newname)"
value="submit">

The Javascript function needs to get the options from the select list:

function mt_showHilite(selname){
var opts=selname.options;

..but I keep getting an error 'options' is null or not an object.

I know the problem lies in my onclick text, but can't figure it out.

A bigger problem lies in the way you've described the problem.
From your example, it looks to me as if you don't really want
to pass either the form name or the select name to the function.
What you seem to want is a reference to the select object.
We are left to assume that the select object's name is "newname".

The "this" keyword in your onclick handler refers to the button.
The button doesn't have an attribute named "newname", but every
form element does have an attribute named "form", which is a
reference to the form that contains it. If that form contains
an element named "newname", what you want is:

onclick="mt_showHilite(this.form.newname)"
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top