Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Pass formname and select name to function

Reply
Thread Tools

Pass formname and select name to function

 
 
Mark
Guest
Posts: n/a
 
      11-03-2003
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!
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-03-2003
Mark <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      11-04-2003
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)"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript : formname.body.value.length == 0 validation planetparag Java 3 02-08-2006 05:01 AM
get dynamic formname and return value to opener drdave@canoemail.com Javascript 5 02-02-2006 11:14 PM
Parent.FrameName.FormName.FormElement.Focus Jon Javascript 0 07-26-2004 08:05 PM
dynamic formname/fieldname geradeaus Javascript 5 04-21-2004 03:46 PM
Problem in document.formName.OnSubmit("return validationCheck(a,b,c)") event used in a function in an ASP Page Varun ASP General 2 03-05-2004 09:39 PM



Advertisments