How can I get the value of a form input whose name is in a Javascript variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neelay1@gmail.com

    How can I get the value of a form input whose name is in a Javascript variable?

    Hi all,
    I have a Javascript variable that contains the name of a form input-
    input_name = "document.myfor m.ip"
    How can I get the value of this form input, "ip" using the variable
    input_name?
    input_name.valu e returns undefined value and input_name + ".value"
    returns the string document.myform .ip.value
    Any help will be highly appreciated.

    Thanks,
    Neelay.

  • andy baxter

    #2
    Re: How can I get the value of a form input whose name is in a Javascript variable?

    On Wed, 07 Jun 2006 17:36:04 -0700, neelay1 wrote:
    [color=blue]
    > Hi all,
    > I have a Javascript variable that contains the name of a form input-
    > input_name = "document.myfor m.ip"[/color]

    Is this the actual code? If so you've made a mistake - all you've done is
    set the variable input_name equal to the /string/ "document.myfor m.id"
    this has nothing to do with the object document.myform .id. Also, I think
    this syntax only works in internet explorer - you're better using
    something like:

    if (document.getEl ementById) {
    input_name=docu ment.getElement ById("ip");
    }

    and setting the 'id' property of the form input to 'ip'
    [color=blue]
    > How can I get the value of this form input, "ip" using the variable
    > input_name?
    > input_name.valu e returns undefined value and input_name + ".value"
    > returns the string document.myform .ip.value
    > Any help will be highly appreciated.
    >
    > Thanks,
    > Neelay.[/color]

    Comment

    • Matt Kruse

      #3
      Re: How can I get the value of a form input whose name is in a Javascript variable?

      neelay1@gmail.c om wrote:[color=blue]
      > I have a Javascript variable that contains the name of a form input-
      > input_name = "document.myfor m.ip"[/color]

      First, reference forms correctly:


      document.myform is not a good idea.
      [color=blue]
      > How can I get the value of this form input, "ip" using the variable
      > input_name?[/color]

      Second, instead of your code, do:

      var form_name = "myform";
      var input_name = "ip";

      then

      var value=document. forms[form_name].elements[input_name].value;

      See http://www.javascripttoolbox.com/bes...#squarebracket

      If there is absolutely no way to avoid having

      input_name = "document.forms .myform.ip";

      then you would need to use eval:

      eval("var value = "+input_name+". value");

      But this is discouraged:


      --
      Matt Kruse




      Comment

      • Richard Cornford

        #4
        Re: How can I get the value of a form input whose name is in a Javascript variable?

        andy baxter wrote:
        <snip>[color=blue]
        > document.myform .id. Also, I think this syntax only works in
        > internet explorer - you're better using something like:
        >
        > if (document.getEl ementById) {
        > input_name=docu ment.getElement ById("ip");
        > }
        >
        > and setting the 'id' property of the form input to 'ip'[/color]
        <snip>

        The 'shortcut' form accessor properties are widely (apparently
        universally) supported in HTML DOMs. The normal recommended method is to
        access form controls through the W3C standard - document.forms -
        collection and then through the - elements - collection of the form
        element (that approach being back-compatible with 'shortcut' supporting
        browsers, W3C DOM standard and available in XHTML DOMs):-

        <URL: http://jibbering.com/faq/faq_notes/form_access.html >

        Richard.


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: How can I get the value of a form input whose name is in aJavascript variable?

          "Matt Kruse" <newsgroups@mat tkruse.com> writes:
          [color=blue]
          > If there is absolutely no way to avoid having
          >
          > input_name = "document.forms .myform.ip";
          >
          > then you would need to use eval:
          >
          > eval("var value = "+input_name+". value");[/color]

          "Need" is such a strong word :)

          You can do it like that, and it's probably also the simplest
          way, but it has absolutely no validation of the format of the
          input string.

          You could parse the string, something like:
          ---
          // tests and captures: document[.forms].<formid>[.elements].<controlname>
          var formRE = /^document(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
          // ...
          var match = input_name.matc h(formRE);
          if (formRE) {
          var value = document.forms[match[1]].elements[match[2]];
          }
          ---
          Then you both test the input and avoid eval.
          [color=blue]
          > But this is discouraged:
          > http://www.javascripttoolbox.com/bes...s/new.php#eval[/color]

          And mostly unnecessary.
          There are *very* few things that can't be done without eval.
          There are almost as few that is best done with eval.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Noah Sussman

            #6
            Re: How can I get the value of a form input whose name is in a Javascript variable?

            Here is an alternative way to parse property names. This method
            doesn't validate the input, but it is generalized, and handles any
            property name that gets passed to it.

            function setNestedProper ty(El, propName, myValue){
            var propertyNameAsL ist = propName.split( '.');
            var myProp = El;
            for (var i=0; i < propertyNameAsL ist.length; i++){
            if (i == (propertyNameAs List.length - 1)){
            myProp[ propertyNameAsL ist[i] ] = myValue;
            return;
            }
            myProp = myProp[ propertyNameAsL ist[i] ];
            }
            }

            setNestedProper ty(myDiv, 'style.display' , 'block');
            setNestedProper ty(myDiv, 'className', 'foo');

            Lasse Reichstein Nielsen wrote:[color=blue]
            > "Matt Kruse" <newsgroups@mat tkruse.com> writes:
            >[color=green]
            > > If there is absolutely no way to avoid having
            > >
            > > input_name = "document.forms .myform.ip";
            > >
            > > then you would need to use eval:
            > >
            > > eval("var value = "+input_name+". value");[/color]
            >
            > "Need" is such a strong word :)
            >
            > You can do it like that, and it's probably also the simplest
            > way, but it has absolutely no validation of the format of the
            > input string.
            >
            > You could parse the string, something like:
            > ---
            > // tests and captures: document[.forms].<formid>[.elements].<controlname>
            > var formRE = /^document(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
            > // ...
            > var match = input_name.matc h(formRE);
            > if (formRE) {
            > var value = document.forms[match[1]].elements[match[2]];
            > }
            > ---
            > Then you both test the input and avoid eval.
            >[color=green]
            > > But this is discouraged:
            > > http://www.javascripttoolbox.com/bes...s/new.php#eval[/color]
            >
            > And mostly unnecessary.
            > There are *very* few things that can't be done without eval.
            > There are almost as few that is best done with eval.
            >
            > /L
            > --
            > Lasse Reichstein Nielsen - lrn@hotpop.com
            > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            > 'Faith without judgement merely degrades the spirit divine.'[/color]

            Comment

            • Noah Sussman

              #7
              Re: How can I get the value of a form input whose name is in a Javascript variable?

              Erm, I guess it would be nicer if I actually posted code that solved
              the problem everyone was discussing in the first place :)

              function getNestedProper tyValue(El, propName){
              var propertyNameAsL ist = propName.split( '.');
              var myProp = El;
              for (var i=0; i < propertyNameAsL ist.length; i++){
              if (i == (propertyNameAs List.length - 1)){
              return myProp[ propertyNameAsL ist[i] ] ;
              }
              myProp = myProp[ propertyNameAsL ist[i] ];
              }
              }

              var fieldValue = getNestedProper tyValue(documen t,
              'forms.postform .textbox.value' );
              var formsList = getNestedProper tyValue(documen t, 'forms');


              Noah Sussman wrote:[color=blue]
              > Here is an alternative way to parse property names. This method
              > doesn't validate the input, but it is generalized, and handles any
              > property name that gets passed to it.
              >
              > function setNestedProper ty(El, propName, myValue){
              > var propertyNameAsL ist = propName.split( '.');
              > var myProp = El;
              > for (var i=0; i < propertyNameAsL ist.length; i++){
              > if (i == (propertyNameAs List.length - 1)){
              > myProp[ propertyNameAsL ist[i] ] = myValue;
              > return;
              > }
              > myProp = myProp[ propertyNameAsL ist[i] ];
              > }
              > }
              >
              > setNestedProper ty(myDiv, 'style.display' , 'block');
              > setNestedProper ty(myDiv, 'className', 'foo');
              >
              > Lasse Reichstein Nielsen wrote:[color=green]
              > > "Matt Kruse" <newsgroups@mat tkruse.com> writes:
              > >[color=darkred]
              > > > If there is absolutely no way to avoid having
              > > >
              > > > input_name = "document.forms .myform.ip";
              > > >
              > > > then you would need to use eval:
              > > >
              > > > eval("var value = "+input_name+". value");[/color]
              > >
              > > "Need" is such a strong word :)
              > >
              > > You can do it like that, and it's probably also the simplest
              > > way, but it has absolutely no validation of the format of the
              > > input string.
              > >
              > > You could parse the string, something like:
              > > ---
              > > // tests and captures: document[.forms].<formid>[.elements].<controlname>
              > > var formRE = /^document(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
              > > // ...
              > > var match = input_name.matc h(formRE);
              > > if (formRE) {
              > > var value = document.forms[match[1]].elements[match[2]];
              > > }
              > > ---
              > > Then you both test the input and avoid eval.
              > >[color=darkred]
              > > > But this is discouraged:
              > > > http://www.javascripttoolbox.com/bes...s/new.php#eval[/color]
              > >
              > > And mostly unnecessary.
              > > There are *very* few things that can't be done without eval.
              > > There are almost as few that is best done with eval.
              > >
              > > /L
              > > --
              > > Lasse Reichstein Nielsen - lrn@hotpop.com
              > > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              > > 'Faith without judgement merely degrades the spirit divine.'[/color][/color]

              Comment

              Working...