Dynamic form content

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

    Dynamic form content

    Hi,

    I have a form on an html page. The first field in the page asks the
    user a enter the number.

    What I want to do then is to present the user with a number of fields
    based on the number entered. So if the user enters '5' I want to
    present him/her with 5 text boxes.

    I can do this on another page by grabbing the number from the field
    and looping through until I hit n. However, I'd really like to write
    the fields onto the same page as the number field using onchange().

    I've tried document.write( ) but that writes the content to a new page.
    I don't think that I can 'hide' the fields through the style because I
    don't want to impose a limit to the number of fields allowed.

    I'd be grateful for any suggestions for writing the content to the
    same page. Maybe using frames?

    Thanks in advance.

    Regards

    Emmett
  • Mick White

    #2
    Re: Dynamic form content

    Emmett Power wrote:

    <snip>[color=blue]
    >
    > What I want to do then is to present the user with a number of fields
    > based on the number entered. So if the user enters '5' I want to
    > present him/her with 5 text boxes.
    >
    > I can do this on another page by grabbing the number from the field
    > and looping through until I hit n. However, I'd really like to write
    > the fields onto the same page as the number field using onchange().
    >
    > I've tried document.write( ) but that writes the content to a new page.
    > I don't think that I can 'hide' the fields through the style because I
    > don't want to impose a limit to the number of fields allowed.
    >[/color]

    You may use: "document.getEl ementById(eleme ntID).innerHTML " instead of
    "document.write ()"

    for(f=0;f<selec tedNumber;f++){
    document.getEle mentById("foo") .innerHTML+=
    "<input type='text' name= 'textfield'"+f+ "' >"
    }
    Mick
    [color=blue]
    > I'd be grateful for any suggestions for writing the content to the
    > same page. Maybe using frames?
    >
    > Thanks in advance.
    >
    > Regards
    >
    > Emmett[/color]

    Comment

    • Emmett Power

      #3
      Re: Dynamic form content

      Mick,

      Thanks for your help. I had to tweak it very slightly to make it work,
      from:

      <input type='text' name= 'textfield'"+f+ "' >

      to:

      <input type='text' name= 'textfield"+f+" ' >

      After that it worked brilliantly.

      Regards

      Emmett

      *** Sent via Devdex http://www.devdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Dr John Stockton

        #4
        Re: Dynamic form content

        JRS: In article <6e11de5c.04062 80158.70571847@ posting.google. com>, seen
        in news:comp.lang. javascript, Emmett Power <Emmett@Silic o-Research.com>
        posted at Mon, 28 Jun 2004 02:58:59 :[color=blue]
        >
        >I'd be grateful for any suggestions for writing the content to the
        >same page.[/color]

        Before posting here, you should read the FAQ. Then, it will often no
        longer be necessary to ask. For this, section 4.15.

        Even if
        document.getEle mentById("aID") .innerHTML= "Some <em>new</em> Content";
        works in all the browsers you care about, I suggest using (if that needs
        to be done in more than one or two places)

        function WriteInto(I, S) { document.getEle mentById(I).inn erHTML = S }

        and later such as WriteInto("aID" , "Some <em>new</em> Content")
        merely for readability.

        For substituting getElementById see the FAQ notes, alternative
        DynWrite page.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • DU

          #5
          Re: Dynamic form content

          Emmett Power wrote:
          [color=blue]
          > Mick,
          >
          > Thanks for your help. I had to tweak it very slightly to make it work,
          > from:
          >
          > <input type='text' name= 'textfield'"+f+ "' >
          >
          > to:
          >
          > <input type='text' name= 'textfield"+f+" ' >
          >
          > After that it worked brilliantly.
          >
          > Regards
          >
          > Emmett
          >[/color]

          I would avoid using innerHTML and I would use exclusively DOM 2 HTML
          attributes and methods to create these inputs and appendChild() to
          insert these in the form node.

          DU

          Comment

          • Mick White

            #6
            Re: Dynamic form content

            DU wrote:

            [color=blue]
            >
            > I would avoid using innerHTML and I would use exclusively DOM 2 HTML
            > attributes and methods to create these inputs and appendChild() to
            > insert these in the form node.
            >
            > DU[/color]

            Why? There is less support for DOM 2.
            Is there some kind of stigma attached to "element.innerH TML"?
            Which browser supports "node.appendChi ld()". and doesn't support innerHTML?

            Mick

            Comment

            • DU

              #7
              Re: Dynamic form content

              Mick White wrote:
              [color=blue]
              > DU wrote:
              >
              >[color=green]
              >>
              >> I would avoid using innerHTML and I would use exclusively DOM 2 HTML
              >> attributes and methods to create these inputs and appendChild() to
              >> insert these in the form node.
              >>
              >> DU[/color]
              >
              >
              > Why? There is less support for DOM 2.[/color]

              appendChild is a DOM 1 Core method which was defined as soon as 1998.

              [color=blue]
              > Is there some kind of stigma attached to "element.innerH TML"?[/color]

              It's not a standard W3C DOM method. innerHTML is not future proof, is
              not forward-compatible; appendChild() is.
              [color=blue]
              > Which browser supports "node.appendChi ld()". and doesn't support innerHTML?
              >
              > Mick[/color]

              I don't know. So why should you *rely* on a proprietary method then?

              DU

              Comment

              • Mick White

                #8
                Re: Dynamic form content

                DU wrote:
                [color=blue]
                > Mick White wrote:
                >[color=green]
                >> DU wrote:[color=darkred]
                >>> I would avoid using innerHTML and I would use exclusively DOM 2 HTML
                >>> attributes and methods to create these inputs and appendChild() to
                >>> insert these in the form node.[/color][/color][/color]
                [color=blue]
                >[color=green]
                >> Which browser supports "node.appendChi ld()". and doesn't support
                >> innerHTML?
                >>
                >> Mick[/color][/color]
                [color=blue]
                > I don't know. So why should you *rely* on a proprietary method then?[/color]

                I see your point, but this method is very likely to be around for the
                foseeable future. And its simplicity of implementation appeals to the
                average "grunt" programmer.
                Thanks for the thoughtful response.
                Mick[color=blue]
                >
                > DU[/color]

                Comment

                • Dr John Stockton

                  #9
                  Re: Dynamic form content

                  JRS: In article <cbqpe4$edb$1@n ews.eusc.inter. net>, seen in
                  news:comp.lang. javascript, DU <drunclear@hotW IPETHISmail.com > posted at
                  Mon, 28 Jun 2004 23:59:03 :[color=blue]
                  >
                  >It's not a standard W3C DOM method. innerHTML is not future proof, is
                  >not forward-compatible; appendChild() is.[/color]

                  In a compiled language, there is generally no need to be compatible with
                  versions of the compiler previous to that being used by the programmer.

                  That is not the case for distributed interpretation of scripts, except
                  on an intranet where browser versions are controlled. It is useful to
                  be compatible with future browsers, even those that do not maintain
                  backward compatibility; but it is more important to be compatible with
                  the browsers that may actually be used at the present time.

                  The operation is a general one, of putting a string at a named location;
                  it should be coded for legibility as a function, and then alteration for
                  revised or increased (forwards, backwards, sideways) can be done at a
                  single location.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                  <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
                  <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                  Comment

                  • DU

                    #10
                    Re: Dynamic form content

                    Dr John Stockton wrote:
                    [color=blue]
                    > JRS: In article <cbqpe4$edb$1@n ews.eusc.inter. net>, seen in
                    > news:comp.lang. javascript, DU <drunclear@hotW IPETHISmail.com > posted at
                    > Mon, 28 Jun 2004 23:59:03 :
                    >[color=green]
                    >>It's not a standard W3C DOM method. innerHTML is not future proof, is
                    >>not forward-compatible; appendChild() is.[/color]
                    >
                    >
                    > That is not the case for distributed interpretation of scripts, except
                    > on an intranet where browser versions are controlled. It is useful to
                    > be compatible with future browsers, even those that do not maintain
                    > backward compatibility; but it is more important to be compatible with
                    > the browsers that may actually be used at the present time.
                    >[/color]

                    Where have I suggested that it is not important to be compatible with
                    current browsers? Which current browser doesn't support appendChild()?

                    DU

                    Comment

                    Working...