how to refer to a form element?

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

    how to refer to a form element?

    Hello,

    I can refer to a form element called value using

    f.B1.value (where B1 is the name of the input)

    but need to put this into the more general form to cover

    f.B2.value, f.B3.value etc

    I wrote

    var name='B'+test_n um;alert(f.name .value);

    and this doesn't work.

    Also tried

    var name='B'+test_n um;alert('f.' + name + '.value');

    but this just gives 'f.' + name + '.value' and not the actual value.

    How do I do this?!

    Cheers

    Geoff
  • Tom de Neef

    #2
    Re: how to refer to a form element?

    "Geoff Cox" <gcox@freeuk.no tcomschreef in bericht
    news:9j7j04p8ls id1npsfnri7d5sk 18icrhk75@4ax.c om...
    Hello,
    >
    I can refer to a form element called value using
    >
    f.B1.value (where B1 is the name of the input)
    >
    but need to put this into the more general form to cover
    >
    f.B2.value, f.B3.value etc
    >
    I wrote
    >
    var name='B'+test_n um;alert(f.name .value);
    >
    and this doesn't work.
    >
    Also tried
    >
    var name='B'+test_n um;alert('f.' + name + '.value');
    >
    but this just gives 'f.' + name + '.value' and not the actual value.
    >
    How do I do this?!
    >
    You can access a propertie via f.B2 but also via f[B2]. In the first case B2
    needs to be the name of the property; in the second case it needs to be (an
    expression that can be evaluated to) a string with the name of the property.
    So, experiment with f['B'+test_num].value
    Tom


    Comment

    • Evertjan.

      #3
      Re: how to refer to a form element?

      Tom de Neef wrote on 19 apr 2008 in comp.lang.javas cript:
      "Geoff Cox" <gcox@freeuk.no tcomschreef in bericht
      news:9j7j04p8ls id1npsfnri7d5sk 18icrhk75@4ax.c om...
      >Hello,
      >>
      >I can refer to a form element called value using
      >f.B1.value (where B1 is the name of the input)
      >but need to put this into the more general form to cover
      >f.B2.value, f.B3.value etc
      >>
      >I wrote
      >var name='B'+test_n um;alert(f.name .value);
      >and this doesn't work.
      >Also tried
      >var name='B'+test_n um;alert('f.' + name + '.value');
      >but this just gives 'f.' + name + '.value' and not the actual value.
      >How do I do this?!
      >
      You can access a propertie via f.B2 but also via f[B2]. In the first
      case B2 needs to be the name of the property; in the second case it
      needs to be (an expression that can be evaluated to) a string with the
      name of the property. So, experiment with f['B'+test_num].value

      Beter use the general approach,
      less error prone, more crossbrowser proof:

      var n = 2;
      var result = document.forms['myForm'].elements['B'+n].value;

      It is a good Idea to read the NG FAQ:

      <http://jibbering.com/faq/#FAQ4_13>
      <http://jibbering.com/faq/#FAQ4_25>

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Geoff Cox

        #4
        Re: how to refer to a form element?

        On Sat, 19 Apr 2008 10:37:15 +0200, "Tom de Neef" <tdeneef@qolor. nl>
        wrote:
        >"Geoff Cox" <gcox@freeuk.no tcomschreef in bericht
        >news:9j7j04p8l sid1npsfnri7d5s k18icrhk75@4ax. com...
        >Hello,
        >>
        >I can refer to a form element called value using
        >>
        >f.B1.value (where B1 is the name of the input)
        >>
        >but need to put this into the more general form to cover
        >>
        >f.B2.value, f.B3.value etc
        >>
        >I wrote
        >>
        >var name='B'+test_n um;alert(f.name .value);
        >>
        >and this doesn't work.
        >>
        >Also tried
        >>
        >var name='B'+test_n um;alert('f.' + name + '.value');
        >>
        >but this just gives 'f.' + name + '.value' and not the actual value.
        >>
        >How do I do this?!
        >>
        >
        >You can access a propertie via f.B2 but also via f[B2]. In the first case B2
        >needs to be the name of the property; in the second case it needs to be (an
        >expression that can be evaluated to) a string with the name of the property.
        >So, experiment with f['B'+test_num].value
        >Tom
        >
        Many thanks Tom, I was going round in the proverbial circles!

        Cheers

        Geoff

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: how to refer to a form element?

          Geoff Cox <gcox@freeuk.no tcomwrites:
          I can refer to a form element called value using
          >
          f.B1.value (where B1 is the name of the input)
          Maybe you can, maybe you can't, but in any case, you shouldn't.
          Use the stanard form:
          document.forms['f'].elements['B1'].value
          but need to put this into the more general form to cover
          >
          f.B2.value, f.B3.value etc
          >
          I wrote
          >
          var name='B'+test_n um;alert(f.name .value);
          >
          and this doesn't work.
          As it shouldn't. You are loking up a property called "name",
          and never referencing the "name" variable.
          >
          Also tried
          >
          var name='B'+test_n um;alert('f.' + name + '.value');
          Here you do string manipulation, and the result is a string.
          but this just gives 'f.' + name + '.value' and not the actual value.
          It gives the value of the string.
          How do I do this?!
          var name = 'B' + test_num;
          var value = document.forms['f'].elements[name].value;

          /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

          • Geoff Cox

            #6
            Re: how to refer to a form element?

            On 19 Apr 2008 08:45:53 GMT, "Evertjan."
            <exjxw.hannivoo rt@interxnl.net wrote:
            >var n = 2;
            >var result = document.forms['myForm'].elements['B'+n].value;
            >
            >It is a good Idea to read the NG FAQ:
            OK thanks Evertjan

            Cheers

            Geoff
            >
            ><http://jibbering.com/faq/#FAQ4_13>
            ><http://jibbering.com/faq/#FAQ4_25>

            Comment

            • Geoff Cox

              #7
              Re: how to refer to a form element?

              On Sat, 19 Apr 2008 11:09:37 +0200, Lasse Reichstein Nielsen
              <lrn@hotpop.com wrote:

              >var name = 'B' + test_num;
              >var value = document.forms['f'].elements[name].value;
              Thanks Lasse,

              Cheers

              Geoff

              Comment

              • fssm2666

                #8
                Re: how to refer to a form element?


                Hi!
                One option is iterating over the collection of elements in the form.
                An example:

                for(var i=0;i<form.elem ents.length;i++ ){
                var nameElement=for m.elements[i].name;
                var valueElement=fo rm.elements[i].value;

                // do something...
                }

                I hope this can help you.

                Felipe Silva.
                Chile.



                On Apr 19, 3:32 am, Geoff Cox <g...@freeuk.no tcomwrote:
                Hello,
                >
                I can refer to a form element called value using
                >
                f.B1.value (where B1 is the name of the input)
                >
                but need to put this into the more general form to cover
                >
                f.B2.value, f.B3.value etc
                >
                I wrote
                >
                var name='B'+test_n um;alert(f.name .value);
                >
                and this doesn't work.
                >
                Also tried
                >
                var name='B'+test_n um;alert('f.' + name + '.value');
                >
                but this just gives 'f.' + name + '.value' and not the actual value.
                >
                How do I do this?!
                >
                Cheers
                >
                Geoff

                Comment

                • Geoff Cox

                  #9
                  Re: how to refer to a form element?

                  On Sun, 20 Apr 2008 11:41:10 -0700 (PDT), fssm2666
                  <fssm2666@gmail .comwrote:
                  >
                  >Hi!
                  >One option is iterating over the collection of elements in the form.
                  >An example:
                  >
                  >for(var i=0;i<form.elem ents.length;i++ ){
                  var nameElement=for m.elements[i].name;
                  var valueElement=fo rm.elements[i].value;
                  >
                  // do something...
                  >}
                  >
                  >I hope this can help you.
                  >
                  >Felipe Silva.
                  Thanks Felipe - will try that out.

                  Cheers

                  Geoff

                  Comment

                  Working...