var for form element name

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

    var for form element name

    Hello,

    I'm trying to pass a var to a function for it to be used as the name of
    a form element.

    IE:

    function doThis(form, elemName) {

    if( form.elemName.v alue == false ) {
    // do something
    }

    }

    I can't seem to figure out how to use the var where the name goes.

    Thanks,

    - D

  • bobzimuta

    #2
    Re: var for form element name

    i was able to use
    function doThis(formName , elemName) {

    if( document.forms[formName].elemName.value == false ) {
    // do something
    }

    }

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: var for form element name

      dstefani wrote:
      [color=blue]
      > I'm trying to pass a var to a function for it to be used as the name of
      > a form element.
      >
      > IE:[/color]

      The user agent does not matter, this feature is supported by all JS-capable
      HTML UAs (part of the so-called DOM Level 0).
      [color=blue]
      > function doThis(form, elemName) {
      >
      > if( form.elemName.v alue == false ) {[/color]

      Use bracket property accessor syntax to access an element of the collection:

      if (form.elements[elemName].value)
      {

      Since the `value' property of objects referring to form controls is of type
      string, it can never be `false'. You could test for the empty string (x ==
      '' or x.length == 0), or you could just use automatic type conversion as I
      did.
      [color=blue]
      > // do something
      > }
      >
      > }
      >
      > I can't seem to figure out how to use the var where the name goes.[/color]

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


      HTH

      PointedEars

      Comment

      • dstefani

        #4
        Re: var for form element name

        Excellent, thanks.

        I haven't had to do any JS stuff in a while. Serious cobwebs!

        Thanks,

        - D

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: var for form element name

          Thomas 'PointedEars' Lahn wrote:
          [color=blue]
          > dstefani wrote:[color=green]
          >> function doThis(form, elemName) {
          >>
          >> if( form.elemName.v alue == false ) {[/color]
          >
          > Use bracket property accessor syntax to access an element of the
          > collection:
          >
          > if (form.elements[elemName].value)
          > {[/color]

          Additional note: Since I do not know which value you are passing for `form',
          I have to consider that it may be a number or string identifying the form
          element by its index, name or ID, instead of a reference to a
          HTMLFormElement object. In this case, you would need

          if (document.forms[form].elements[elemName].value ...)
          {
          // ...
          }


          PointedEars

          P.S.: Please quote what you are replying to, see
          <http://jibbering.com/faq/>.

          Comment

          • dstefani

            #6
            Re: var for form element name

            Well the above works, but now I'm trying to go back and focus on the
            errant field, like so:

            <code>

            if ( TE_digit(form.e lements[elemName].value) == false) {
            alert("Yatta, yatta, yatta");
            var elem = form.elements[elemName];
            elem.focus();
            return false;
            }
            </code>

            It still won't focus back on the field that it tested.

            What am I missing?

            Thanks

            - D

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: var for form element name

              dstefani wrote:
              [color=blue]
              > Well the above works,[/color]

              Which "above" are you talking about?
              <http://www.jibbering.c om/faq/faq_notes/pots1.html#ps1P ost>
              [color=blue]
              > but now I'm trying to go back and focus on the
              > errant field, like so:
              >
              > <code>
              >
              > if ( TE_digit(form.e lements[elemName].value) == false) {
              > alert("Yatta, yatta, yatta");
              > var elem = form.elements[elemName];
              > elem.focus();
              > return false;
              > }
              > </code>
              >
              > It still won't focus back on the field that it tested.[/color]

              Probably there is more than one form control with that name in
              which case all so-named form controls are members of a collection
              returned by form.elements[elemName]. The JavaScript console should
              show an script error or Exception on the elem.focus() line then.

              Otherwise you will have to show more code, best by posting a public
              URI for your HTML document.


              PointedEars

              Comment

              Working...