Pass form name and value to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sen

    Pass form name and value to a function

    This is the Javascript function:
    function auto_submit(for m_name, value) {
    document.form_n ame.elements[value].click();
    }

    This is the XHTML:
    <select name="category" onchange="auto_ submit(this.for m.name,'Select' )">

    So, when choosing an option from the select list, the Select button is
    clicked and the form is submitted. Somehow the function only works when
    I replace form_name variable with the name of the form. Ex.
    document.keycha in_selector.ele ments[value].click();

    How can this be solved?

  • Michael Winter

    #2
    Re: Pass form name and value to a function

    On Fri, 26 Nov 2004 14:45:43 +0100, Sen <me@privacy.net > wrote:
    [color=blue]
    > This is the Javascript function:
    > function auto_submit(for m_name, value) {
    > document.form_n ame.elements[value].click();
    > }
    >
    > This is the XHTML:
    > <select name="category"
    > onchange="auto_ submit(this.for m.name,'Select' )">[/color]

    [snip]
    [color=blue]
    > How can this be solved?[/color]

    By passing a reference, not the name:

    function auto_submit(for m, elem) {
    form.elements[elem].click();
    }

    <select ... onchange="auto_ submit(this.for m, 'Select');">

    However, I'd like to point out that performing an action based upon
    selection from a SELECT element is generally a bad idea. It's usually much
    better to allow the user to perform the action themselves with a button.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • ZER0

      #3
      Re: Pass form name and value to a function

      On Fri, 26 Nov 2004 14:45:43 +0100, Sen wrote:
      [color=blue]
      > This is the Javascript function:[/color]
      [color=blue]
      > function auto_submit(for m_name, value) {
      > document.form_n ame.elements[value].click();
      > }[/color]

      form_name is a string, like your "value" variable, not a form's reference.
      So:
      document.forms[form_name].elements[value].click();

      But you can directly use a reference to form:

      <select name="category" onchange="auto_ submit(this.for m,'Select')">

      function auto_submit(f, value) {
      f.elements[value].click();
      }

      or a reference to button:

      <select name="category" onchange="auto_ submit(this.for m.Select)">

      function auto_submit(btn ) {
      btn.click();
      }

      or:

      <select name="category" onchange="this. form.Select.cli ck()">
      [color=blue]
      > So, when choosing an option from the select list, the Select button is
      > clicked and the form is submitted.[/color]

      But why you don't use the submit method of form object?

      <select name="category" onchange="this. form.submit()">

      --
      ZER0://coder.gfxer.web-designer/

      ~ Alcune persone sono ancora vive per il semplice motivo che e'
      illegale ucciderle.

      Comment

      • Sen

        #4
        Re: Pass form name and value to a function

        On 2004-11-26 15:19:31 +0100, "Michael Winter"
        <M.Winter@bluey onder.co.invali d> said:
        [color=blue]
        > By passing a reference, not the name:
        >
        > function auto_submit(for m, elem) {
        > form.elements[elem].click();
        > }
        >
        > <select ... onchange="auto_ submit(this.for m, 'Select');">[/color]

        Thanks! That worked perfectly :)
        [color=blue]
        > However, I'd like to point out that performing an action based upon
        > selection from a SELECT element is generally a bad idea. It's usually
        > much better to allow the user to perform the action themselves with a
        > button.[/color]

        I fully agree useability & standards wise but the form isn't made for a
        wide audience. It's a keychain web app to store my passwords . I can
        work a little speedier with this solution.

        Comment

        • Sen

          #5
          Re: Pass form name and value to a function

          On 2004-11-26 15:28:33 +0100, ZER0 <zer0.shock@lib ero.it> said:
          [color=blue]
          > form_name is a string, like your "value" variable, not a form's reference.
          > So:[/color]
          [color=blue]
          > [...][/color]
          [color=blue]
          > or a reference to button:[/color]
          [color=blue]
          > [...][/color]
          [color=blue]
          > or:[/color]
          [color=blue]
          > [...][/color]

          All solution work perfectly :) but I prefer the shortest one.
          [color=blue]
          > But why you don't use the submit method of form object?
          > <select name="category" onchange="this. form.submit()">[/color]

          Because I have three submit buttons with different values. Form.submit
          doesn't send any value to the cgi.

          Comment

          Working...