Setting forms using Javascript

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

    Setting forms using Javascript

    Hi All,

    Been learning Javascript (via google) to create forms with pre-set values.
    As seen below. My problem I have found is this: I can't seem to figure out
    (or find so far via google) how to preset form options when the input type
    is a radio button, or dropdown list. The same method does not seem to work
    with input fields.

    Anyone know of such a comprehensive site?

    Thanks ahead,

    Daniel

    <FORM name="PersProf" action="newuser .pl" method="POST">

    <table border="0">
    <tr>
    <td WIDTH="200">
    First Name
    </td>
    <td>
    <input type="text" name="first_nam e" maxlength=32 size = 32>
    </td>
    </tr>
    </table>


    <script language="JavaS cript">
    <!--
    //setting the value:
    document.PersPr of.first_name.v alue = '$first_name';
    -->
    </script>


  • Ulrik Skovenborg

    #2
    Re: Setting forms using Javascript

    daniel kaplan wrote:[color=blue]
    > My problem I have found is this: I can't seem to figure out
    > (or find so far via google) how to preset form options when the input type
    > is a radio button, or dropdown list. The same method does not seem to work
    > with input fields.[/color]

    Do you mean preselected:
    <input type="radio" checked> and
    <option selected value="val">tex t</option>

    Comment

    • Michael Winter

      #3
      Re: Setting forms using Javascript

      On Sat, 11 Dec 2004 09:45:59 -0500, daniel kaplan <nospam@nospam. com>
      wrote:
      [color=blue]
      > Hi All,
      >
      > Been learning Javascript (via google) to create forms
      > with pre-set values.[/color]

      You don't specify initial values with Javascript. You specify them in HTML:

      <input type="text" ... value="initial value">

      <input type="checkbox" ... checked>

      <input type="radio" ... checked>

      <select ...>
      <option ... selected>...</option>
      </select>

      <textarea ...>initial value</textarea>

      I suggest you read the HTML Specification
      (<URL:http://www.w3.org/TR/html4/>), rather than rely on the Web.
      Information regarding forms can be found under section 17
      (<URL:http://www.w3.org/TR/html4/interact/forms.html>).

      If you have any further problems, please ask in alt.html or
      comp.infosystem s.www.authoring.html.

      [snip]
      [color=blue]
      > <script language="JavaS cript">[/color]

      The language attribute has been deprecated for over six years. Use the
      type attribute instead:

      <script type="text/javascript">
      [color=blue]
      > <!--[/color]

      Hiding scripts with SGML comment delimiters is an obsolete practice. It's
      not necessary.
      [color=blue]
      > //setting the value:
      > document.PersPr of.first_name.v alue = '$first_name';[/color]

      The preferred way to access form controls is via the forms and elements
      collections:

      document.forms['form-id'].elements['control-name-or-id']

      [snip]

      Good luck,
      Mike

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

      Comment

      • daniel kaplan

        #4
        Re: Setting forms using Javascript

        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opsium76g8 x13kvk@atlantis ...[color=blue]
        >
        > You don't specify initial values with Javascript. You specify them in[/color]
        HTML:

        i guess since i was able to set the initial value of a TEXT field using
        javascript, that i could do the same with ALL fields in the form...

        and i using perl to generate the HTML dynamically, so my perl build the
        html, with javascript included...

        i can set them using HTML, but to do it dynamcially i turned to java....

        strange that it allows me to do it for text and nothing else, no?

        daniel


        Comment

        • Ivo

          #5
          Re: Setting forms using Javascript

          "daniel kaplan" wrote[color=blue]
          > i guess since i was able to set the initial value of a TEXT field using
          > javascript, that i could do the same with ALL fields in the form...[/color]

          Being able to do something is never a good reason to go do it. In
          programming the question should be: is it necessary? Here the answer is a
          crystal clear no, see below.
          [color=blue]
          > and i using perl to generate the HTML dynamically, so my perl build the
          > html, with javascript included...
          >
          > i can set them using HTML, but to do it dynamcially i turned to java....[/color]

          Java? I didn't see any java in your earlier code. It 's javascript, a
          totally different and independent language. The names are confusing,
          no one disagrees with that, but the environments in which they operate,
          are incomparable, even within the same webbrowser.
          [color=blue]
          > strange that it allows me to do it for text and nothing else, no?[/color]

          It does allow you to do it for all types of input elements. The point is
          that you don't need javascript at all. Let your perl script write the values
          directly in the value attributes of the elements. From your first post I
          gather that in the perl syntax it would look like this:

          <input type="text" name="first_nam e" value="$first_n ame">

          Look Ma, no javascript!
          --
          Ivo





          Comment

          • daniel kaplan

            #6
            Re: Setting forms using Javascript

            "Ivo" <no@thank.you > wrote in message
            news:41bb5092$0 $8919$abc4f4c3@ news.wanadoo.nl ...
            [color=blue]
            > Being able to do something is never a good reason to go do it.[/color]
            well, it seemed that javascript was rich Forms features. of course am
            totally new to it, so that is how it appears to me, it coudl be wrong

            [color=blue]
            > Java? I didn't see any java in your earlier code.[/color]
            obviously i typo'ed there
            [color=blue]
            > It does allow you to do it for all types of input elements.[/color]
            do you know a decent website which points this out? even if i don't end up
            using that part of it, i would still like to learn it.

            [color=blue]
            >The point is that you don't need javascript at all. Let your perl script[/color]
            write the values[color=blue]
            > directly in the value attributes of the elements. From your first post I
            > gather that in the perl syntax it would look like this:
            >
            > <input type="text" name="first_nam e" value="$first_n ame">[/color]

            that is how i started...but when it came to other forms features such as a
            drop down list of states, where one might already be sleceted, it obviously
            was going to be much more coding. i.e. (OPTION vs. SELECTED) so that's why
            i turned to look at javascript.

            thanks,

            daniel


            Comment

            • Randy Webb

              #7
              Re: Setting forms using Javascript

              daniel kaplan wrote:[color=blue]
              > "Ivo" <no@thank.you > wrote in message
              > news:41bb5092$0 $8919$abc4f4c3@ news.wanadoo.nl ...
              >
              >[color=green]
              >>The point is that you don't need javascript at all. Let your perl script
              >>write the values directly in the value attributes of the elements. From
              >> your first post I gather that in the perl syntax it would look like this:
              >>
              >><input type="text" name="first_nam e" value="$first_n ame">[/color]
              >
              >
              > that is how i started...but when it came to other forms features such as a
              > drop down list of states, where one might already be sleceted, it obviously
              > was going to be much more coding. i.e. (OPTION vs. SELECTED) so that's why
              > i turned to look at javascript.[/color]

              Hava Perl generate the select element. As it loops through the elements
              of an array to create it, have it check the value, if they match, then
              add the selected.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              • daniel kaplan

                #8
                Re: Setting forms using Javascript

                "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
                news:vMGdnRkb18 V85SbcRVn-qQ@comcast.com. ..
                [color=blue]
                > Hava Perl generate the select element. As it loops through the elements
                > of an array to create it, have it check the value, if they match, then
                > add the selected.[/color]

                well i am doing that with a lot of Ternery statements, because i am using
                perl to build out the entire HTML page...it's just messy, so i assumed that
                using javascript to do it at the end would have been cleaner


                Comment

                • Ivo

                  #9
                  Re: Setting forms using Javascript

                  "Randy Webb" wrote[color=blue]
                  > daniel kaplan wrote:"[color=green]
                  > > "Ivo" <no@thank.you > wrote[color=darkred]
                  > > > Being able to do something is never a good reason to go do it.[/color]
                  > > well, it seemed that javascript was rich Forms features. of course am
                  > > totally new to it, so that is how it appears to me, it coudl be wrong[/color][/color]

                  The rule applies to all things. As far as javascript is concerned, you 'll
                  stop being new after reading the FAQ of this newsgroup


                  and related documents, and you understand why the sport is to rely on
                  javascript as little as possible. You control what happens on the server,
                  but when a page reaches the client, you just don't know what browser on what
                  OS is claiming to be some other browser, scripts may even be rewritten by
                  proxies and plugins (think popup blockers) and by the user himself. Most
                  importantly, a lot of people surf with javascript disabled or simply not
                  available, by choice or otherwise (estimates vary between 5%-25%). So you
                  want your page to be complete when you send it, and use javascript only for
                  things that are impossible without javascript.
                  [color=blue]
                  > Hava Perl generate the select element. As it loops through the elements
                  > of an array to create it, have it check the value, if they match, then
                  > add the selected.[/color]

                  Here is how I do them in PHP:

                  <?
                  function writeselect($na me,$atb='',$ops =array(),$sel=' '){
                  $s='<select name="'.$name.' " id="'.$name.'" '.$atb.'>';
                  $t=0; foreach($ops as $n=>$v) { $s.='<option
                  value="'.($t=== $n?$v:$n).'"'.( ($v==$sel)?' selected':'').' >'.$v; $t++;}
                  return $s.'</select>';
                  }
                  ?>
                  <label for="cat" title="Grouping by topic">Category : </label>
                  <?=writeselect( 'cat','class="p ut"',array('Art ', 'Business',
                  'Computing', 'General', 'Travel'),'Gene ral');?>
                  <?=writeselect( 'amount','oncha nge="foo(this); "',array('Amoun t', 1=>'One',
                  12=>'Dozen'));? >

                  --
                  Ivo










                  Comment

                  • Randy Webb

                    #10
                    Re: Setting forms using Javascript

                    daniel kaplan wrote:
                    [color=blue]
                    > "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
                    > news:vMGdnRkb18 V85SbcRVn-qQ@comcast.com. ..
                    >
                    >[color=green]
                    >>Hava Perl generate the select element. As it loops through the elements
                    >>of an array to create it, have it check the value, if they match, then
                    >>add the selected.[/color]
                    >
                    >
                    > well i am doing that with a lot of Ternery statements, because i am using
                    > perl to build out the entire HTML page...it's just messy, so i assumed that
                    > using javascript to do it at the end would have been cleaner[/color]

                    As Ivo has said, never rely on JS. Use it as an addition but never a
                    requirement.

                    --
                    Randy
                    comp.lang.javas cript FAQ - http://jibbering.com/faq

                    Comment

                    • daniel kaplan

                      #11
                      Re: Setting forms using Javascript

                      "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
                      news:AcWdnT_bTe 0STybcRVn-hA@comcast.com. ..[color=blue]
                      > daniel kaplan wrote:[/color]
                      [color=blue]
                      > As Ivo has said, never rely on JS. Use it as an addition but never a
                      > requirement.[/color]

                      thanks all for the advice...

                      daniel


                      Comment

                      • Dr John Stockton

                        #12
                        Re: Setting forms using Javascript

                        JRS: In article <1102792693.597 863@nntp.acecap e.com>, dated Sat, 11 Dec
                        2004 14:18:40, seen in news:comp.lang. javascript, daniel kaplan
                        <nospam@nospam. com> posted :[color=blue]
                        >"Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                        >news:opsium76g 8x13kvk@atlanti s...[color=green]
                        >>
                        >> You don't specify initial values with Javascript. You specify them in[/color]
                        >HTML:
                        >
                        >i guess since i was able to set the initial value of a TEXT field using
                        >javascript, that i could do the same with ALL fields in the form...[/color]

                        You cannot set the initial value of an HTML text field with javascript;
                        you can only replace it - before the user notices!

                        The same goes for other controls.

                        AFAIK, in javascript you can re-set the value of any identifiable
                        control that exists. Even an isolated radio-button can, ISTM, be cleared
                        by code.
                        // // // document.write( "<input type=radio name=X> Do Not Touch") // !!

                        In javascript, you can output HTML, including creation of controls with
                        computed initial values; but that is not quite the same thing.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of 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

                        • David Dorward

                          #13
                          Re: Setting forms using Javascript

                          daniel kaplan wrote:
                          [color=blue]
                          > well i am doing that with a lot of Ternery statements, because i am using
                          > perl to build out the entire HTML page...it's just messy[/color]

                          Try installing Template::Toolk it, it makes things rather cleaner.

                          In the Perl you would build a data structure (possibly from a database) that
                          looks something like:

                          $myHashRef = {
                          selectItems => {
                          one => {
                          value => 1,
                          label => 'Thingy',
                          },
                          two => {
                          value => 'wibble',
                          label => 'Wibble',
                          },
                          foo => {
                          value => 3,
                          label => 'Baz',
                          select => 1,
                          },
                          },
                          };

                          and include something like this in the template:

                          <select name="mySelect" >
                          [% foreach item = selectItems %]
                          <option value="[% item.value %]"[% IF item.select %] selected[% END %]>
                          [% item.label %]
                          </option>
                          [% END %]
                          </select>

                          --
                          David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
                          Home is where the ~/.bashrc is

                          Comment

                          Working...