"object doesn't support this property or method" error

Discussion in 'Javascript' started by SmittyBroham, Jan 7, 2005.

  1. SmittyBroham

    SmittyBroham Guest

    Hello,

    I have a function that loops through 2 select lists and records the
    values of any hi-lighted options a user would have selected. It then
    sets 2 corresponding "hidden" form elements to the values and submits
    the form data to the server.

    I was error free until I added the following line:

    document.myform.submit();

    This produces the "object doesn't support this property or method"
    error. The troubleshooting steps I have taken are:

    1) Confirmed the existance of <form> and </form> tags.
    2) Confirmed there is no typo in the form name.
    3) Tried changing the form name from "bhd2pib_form" to just "myform".
    4) Commented out the suspected offending line of code and I am again
    error free, uncomment it, the error comes back.
    5) Added a normal "submit" button to test the action and post
    attributes of the <form> tag and that works fine.

    I am having a hard time believing a simple method call such as
    document.myform.submit(); is so difficult to figure out! Please, any
    help would be greatly appreciated. The function is pasted below in
    it's entirety:

    <script type="text/javascript">
    function setValues()
    {
    var bhd_list = document.myform.bhd_headings;
    var pib_list = document.myform.pib_headings;

    var bhd_values = ''; // String of selected base_heading ids to
    map
    var pib_value;

    for ( i=1; i<bhd_list.options.length; i++ ) {
    if ( bhd_list.options.selected ) {
    if ( !bhd_values ) {
    bhd_values = bhd_values +
    bhd_list.options.value;
    }
    else {
    bhd_values = bhd_values + ':' +
    bhd_list.options.value;
    }
    }
    }

    for ( j=1; j<pib_list.options.length; j++ ) {
    if ( pib_list.options[j].selected ) {
    pib_value = pib_list.options[j].value;
    break;
    }
    }

    if ( !bhd_values ) {
    alert( "You have to select at least 1 base heading to
    map or else this page doesn't make a lot of sense to have." );
    }

    if ( !pib_value ) {
    alert( "You have to select a PIB heading to map to" );
    }

    // Set values of hidden fields
    document.myform.bhd_values.value = bhd_values;
    document.myform.pib_value.value = pib_value;

    document.myform.submit(); //this line causes the error
    }
    </script>
     
    SmittyBroham, Jan 7, 2005
    #1
    1. Advertising

  2. SmittyBroham

    RobG Guest

    SmittyBroham wrote:
    > Hello,
    >
    > I have a function that loops through 2 select lists and records the
    > values of any hi-lighted options a user would have selected. It then
    > sets 2 corresponding "hidden" form elements to the values and submits
    > the form data to the server.
    >
    > I was error free until I added the following line:
    >
    > document.myform.submit();


    I would add your function to the form "onsubmit" event and return
    false if your validation fails. Then your submit stays as a
    plain submit button.

    [...]
    > var bhd_list = document.myform.bhd_headings;
    > var pib_list = document.myform.pib_headings;


    If you pass a reference to the table from the event, you don't
    need all those document.myform calls:

    <form ... onsubmit="return setValues(this);" ... >

    > for ( i=1; i<bhd_list.options.length; i++ ) {


    This can be optimised a little as:

    var len=bhd_list.options.length;
    for (var i=0; i<len; i++) {

    Use var to keep i local. Getting the length property once is
    more efficient and will make a small difference if you have many
    options.

    > if ( bhd_list.options.selected ) {
    > if ( !bhd_values ) {
    > bhd_values = bhd_values +
    > bhd_list.options.value;
    > }
    > else {
    > bhd_values = bhd_values + ':' +
    > bhd_list.options.value;


    This too can be a little more concise:

    for ( i=1; i<bhd_list.options.length; i++ ) {
    if ( bhd_list.options.selected ) {
    if ( !bhd_values == '') bhd_values += ':';
    bhd_values += bhd_list.options.value;
    }
    }

    [...]
    > if ( !bhd_values ) {
    > alert( "You have to select at least 1 base heading to
    > map or else this page doesn't make a lot of sense to have." );
    > }


    return false here to cancel the submit:

    map or else this page doesn't make a lot of sense to have." );
    return false
    }

    > if ( !pib_value ) {
    > alert( "You have to select a PIB heading to map to" );
    > }


    And do the same here


    > // Set values of hidden fields
    > document.myform.bhd_values.value = bhd_values;
    > document.myform.pib_value.value = pib_value;


    Use "theForm" reference here to get rid of document...

    theForm.bhd_values.value = bhd_values;
    theForm.pib_value.value = pib_value;

    >
    > document.myform.submit(); //this line causes the error


    This line is no longer needed. The trivial line to add is:

    return true;

    But that should not be required (but test in lots of browsers
    first).

    The full script is below, tested in IE and Firefox.

    <script type="text/javascript">
    function setValues(theForm) {
    var bhd_list = theForm.bhd_headings,
    pib_list = theForm.pib_headings,
    bhd_values = '',
    pib_value = '';

    var len = bhd_list.options.length;
    for (var i=1; i<len; i++ ) {
    if ( bhd_list.options.selected ) {
    if ( !bhd_values == '') bhd_values += ':';
    bhd_values += bhd_list.options.value;
    }
    }

    var len = pib_list.options.length
    for (var j=1; j<len; j++ ) {
    if ( pib_list.options[j].selected ) {
    pib_value = pib_list.options[j].value;
    break;
    }
    }

    if ( bhd_values == '' ) {
    alert("You have to select at least 1 base"
    + " heading to map or else this page"
    + " doesn't make a lot of sense to have.");
    return false;
    }

    if ( pib_value == '' ) {
    alert("You have to select a PIB heading"
    + " to map to");
    return false;
    }

    // Set values of hidden fields
    theForm.bhd_values.value = bhd_values;
    theForm.pib_value.value = pib_value;
    }
    </script>

    --
    Rob
     
    RobG, Jan 7, 2005
    #2
    1. Advertising

  3. SmittyBroham

    SmittyBroham Guest

    Hi Rob,

    Using the form's onSubmit action did indeed work. Thanks for your
    detailed response!

    -Mark
     
    SmittyBroham, Jan 7, 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. Roman
    Replies:
    0
    Views:
    4,041
    Roman
    Jan 4, 2004
  2. mygoogleac
    Replies:
    0
    Views:
    187
    mygoogleac
    Oct 2, 2003
  3. Matt
    Replies:
    3
    Views:
    212
  4. news.onetel.net.uk
    Replies:
    3
    Views:
    226
    Michael Winter
    Aug 9, 2004
  5. Drew
    Replies:
    1
    Views:
    106
    Martin Honnen
    Nov 8, 2006
Loading...

Share This Page