Anyone know why this javascript doesn't work?

Discussion in 'HTML' started by Ed Jay, Dec 16, 2005.

  1. Ed Jay

    Ed Jay Guest

    When I run the below js, I get the error:
    "document.form1.elements[menu_name+(i)].checked) is null or not an object"

    <script type="text/javascript">

    function get_menu_value(menu_name) {

    var mValue = 0;

    for(var i=0; i < menu_name.length; i++) {

    if (document.form1.elements[menu_name+(i)].checked) {

    mValue = mValue + document.form1.elements[menu_name+(i)].value;
    }
    }
    alert(mValue);
    return false;
    }
    </script>

    <form name = "form1">
    Choice (11)<input type=checkbox name=choice value=11><br>
    Choice (13)<input type=checkbox name=choice value=13><br>
    Choice (15)<input type=checkbox name=choice value=15><br>
    Choice (19)<input type=checkbox name=choice value=19><br>
    Choice (100)<input type=checkbox name=choice value=100><br>
    <input type = "submit" onclick="get_menu_value('choice')">
    </form>

    --
    Ed Jay (remove M to respond by email)
     
    Ed Jay, Dec 16, 2005
    #1
    1. Advertising

  2. On 16/12/2005 18:27, Ed Jay wrote:

    [snip]

    > function get_menu_value(menu_name) {
    >
    > var mValue = 0;
    >
    > for(var i=0; i < menu_name.length; i++) {


    Why does the length of the name determine the number of iterations of
    this loop?

    > if (document.form1.elements[menu_name+(i)].checked) {


    The expression in the square brackets will be evaluated, converted to a
    string, and used as a property name. In your example, these names will
    be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
    checkboxes (you create six names), none of these names match a control
    within the form, resulting in an undefined value, and an error with the
    subsequent property access.

    > mValue = mValue + document.form1.elements[menu_name+(i)].value;


    The value property is a string. The addition operator, with a string and
    a number operand, will perform a concatenation. I suspect that you want
    arithmetic addition.

    [snip]

    > return false;


    [snip]

    > <input type = "submit" onclick="get_menu_value('choice')">


    As you do not return the value returned by the get_menu_value function,
    it will have no effect. In any case, it is generally better to use the
    submit event of the form, passing a reference to the form, rather than
    the click event of the button.

    function getMenuValue(menuName, form) {
    var value = 0,
    menu = form.elements[menuName];

    for(var i = 0, n = menu.length; i < n; ++i) {
    if(menu.checked) {
    /* Unary plus performs explicit conversion to Number. */
    value += +menu.value;
    }
    }
    alert(value);

    return false;
    }

    <form action="..." onsubmit="return getMenuValue('choice', this);">
    <!-- ... -->
    </form>

    Hope that helps,
    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Dec 16, 2005
    #2
    1. Advertising

  3. Ed Jay

    Ed Jay Guest

    Michael Winter <> wrote:

    >On 16/12/2005 18:27, Ed Jay wrote:
    >
    >[snip]
    >
    >> function get_menu_value(menu_name) {
    >>
    >> var mValue = 0;
    >>
    >> for(var i=0; i < menu_name.length; i++) {

    >
    >Why does the length of the name determine the number of iterations of
    >this loop?
    >
    >> if (document.form1.elements[menu_name+(i)].checked) {

    >
    >The expression in the square brackets will be evaluated, converted to a
    >string, and used as a property name. In your example, these names will
    >be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
    >checkboxes (you create six names), none of these names match a control
    >within the form, resulting in an undefined value, and an error with the
    >subsequent property access.
    >
    >> mValue = mValue + document.form1.elements[menu_name+(i)].value;

    >
    >The value property is a string. The addition operator, with a string and
    >a number operand, will perform a concatenation. I suspect that you want
    >arithmetic addition.
    >
    >[snip]
    >
    >> return false;

    >
    >[snip]
    >
    >> <input type = "submit" onclick="get_menu_value('choice')">

    >
    >As you do not return the value returned by the get_menu_value function,
    >it will have no effect. In any case, it is generally better to use the
    >submit event of the form, passing a reference to the form, rather than
    >the click event of the button.
    >
    > function getMenuValue(menuName, form) {
    > var value = 0,
    > menu = form.elements[menuName];
    >
    > for(var i = 0, n = menu.length; i < n; ++i) {
    > if(menu.checked) {
    > /* Unary plus performs explicit conversion to Number. */
    > value += +menu.value;
    > }
    > }
    > alert(value);
    >
    > return false;
    > }
    >
    > <form action="..." onsubmit="return getMenuValue('choice', this);">
    > <!-- ... -->
    > </form>
    >
    >Hope that helps,
    >

    It sure does, Mike. Thank you very much. I'm using your solution for menus
    where all checkboxes have the same name. (I'm going to use Evertjan's for
    those menus where the names are different, e.g., choice1, choice2...)

    --
    Ed Jay (remove M to respond by email)
     
    Ed Jay, Dec 16, 2005
    #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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,129
    Smokey Grindel
    Dec 2, 2006
  2. KathyB
    Replies:
    5
    Views:
    112
    Allan W
    Oct 12, 2003
  3. Simon Egginton

    LOOK! i just want to know does anyone know...

    Simon Egginton, Jul 26, 2004, in forum: Javascript
    Replies:
    3
    Views:
    188
    Dr John Stockton
    Jul 26, 2004
  4. Iver Erling Årva

    Can anyone explain why this doesn't work?

    Iver Erling Årva, Sep 9, 2004, in forum: Javascript
    Replies:
    4
    Views:
    125
    Iver Erling Årva
    Sep 15, 2004
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    262
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page