Pass formname and select name to function

Discussion in 'Javascript' started by Mark, Nov 3, 2003.

  1. Mark

    Mark Guest

    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!
    Mark, Nov 3, 2003
    #1
    1. Advertising

  2. Mark <> 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.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.

    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 3, 2003
    #2
    1. Advertising

  3. Mark

    Lee Guest

    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)"
    Lee, Nov 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. planetparag
    Replies:
    3
    Views:
    740
    Rhino
    Feb 8, 2006
  2. Varun
    Replies:
    2
    Views:
    227
    Varun
    Mar 5, 2004
  3. geradeaus

    dynamic formname/fieldname

    geradeaus, Apr 21, 2004, in forum: Javascript
    Replies:
    5
    Views:
    97
    Grunken
    Apr 21, 2004
  4. Jon
    Replies:
    0
    Views:
    77
  5. Replies:
    5
    Views:
    95
    mick white
    Feb 2, 2006
Loading...

Share This Page