By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,651 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

Pass formname and select name to function

P: n/a
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,

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mark <an*******@devdex.com> writes:
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.ele ments[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.

--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
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)"

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.