how to render an option value dynamic !

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

    #1

    how to render an option value dynamic !

    I have to insert in a html select the last 10 years

    <select name="year" onChange="month ()" size=5>
    <option value="1994">19 94</option>
    <option value="1995">19 95</option>
    <option value="1996">19 96</option>
    <option value="1997">19 97</option>
    <option value="1998">19 98</option>
    <option value="1999">19 99</option>
    <option value="2000">20 00</option>
    <option value="2001">20 01</option>
    <option value="2002">20 02</option>
    <option value="2003">20 03</option>
    <option value="2004">20 04</option>
    </select>

    I did this way. I d like to do it dynamically in javascript but I have
    no idea how to insert the result of a function in an option value.
    Please help
    Gianni
  • rf

    #2
    Re: how to render an option value dynamic !


    "Gianni" <pincopallo_it@ yahoo.it> wrote in message
    news:3c03ef0c.0 407130145.6b4dd edd@posting.goo gle.com...[color=blue]
    > I have to insert in a html select the last 10 years
    >
    > <select name="year" onChange="month ()" size=5>
    > <option value="1994">19 94</option>
    > <option value="1995">19 95</option>
    > <option value="1996">19 96</option>
    > <option value="1997">19 97</option>
    > <option value="1998">19 98</option>
    > <option value="1999">19 99</option>
    > <option value="2000">20 00</option>
    > <option value="2001">20 01</option>
    > <option value="2002">20 02</option>
    > <option value="2003">20 03</option>
    > <option value="2004">20 04</option>
    > </select>
    >
    > I did this way. I d like to do it dynamically in javascript but I have
    > no idea how to insert the result of a function in an option value.[/color]

    You have already written it. Why do you want to now automate it?

    --
    Cheers
    Richard.


    Comment

    • Dag Sunde

      #3
      Re: how to render an option value dynamic !

      "rf" <rf@.invalid> wrote in message
      news:IwOIc.9214 4$sj4.39737@new s-server.bigpond. net.au...[color=blue]
      >
      > "Gianni" <pincopallo_it@ yahoo.it> wrote in message
      > news:3c03ef0c.0 407130145.6b4dd edd@posting.goo gle.com...[color=green]
      > > I have to insert in a html select the last 10 years
      > >
      > > <select name="year" onChange="month ()" size=5>
      > > <option value="1994">19 94</option>
      > > <option value="1995">19 95</option>
      > > <option value="1996">19 96</option>
      > > <option value="1997">19 97</option>
      > > <option value="1998">19 98</option>
      > > <option value="1999">19 99</option>
      > > <option value="2000">20 00</option>
      > > <option value="2001">20 01</option>
      > > <option value="2002">20 02</option>
      > > <option value="2003">20 03</option>
      > > <option value="2004">20 04</option>
      > > </select>
      > >
      > > I did this way. I d like to do it dynamically in javascript but I have
      > > no idea how to insert the result of a function in an option value.[/color]
      >
      > You have already written it. Why do you want to now automate it?
      >[/color]

      Eh... 2005?


      Here's one way to do it:

      Put <div id='tenYearsCom boLayer'></div> on the page where you want your
      combo.

      then execute the following code in the page's onload() event:

      function drawTenYearComb o(){

      var comboLayer = document.getEle mentById('tenYe arsComboLayer') ;
      var buffer = "<select name='year' onChange='month ();' size='5'>";

      var now = new Date();
      var thisYear = now.getFullYear ();

      for( var n=10; n>0; n--)
      buffer += "<option value='" + (thisYear - n) + "'>" + (thisYear - n) +
      "</option>";

      buffer += "</select>";
      comboLayer.inne rHTML = buffer;

      }

      --
      Dag
      58°26'15.9" N 008°46'45.5" E


      Comment

      • Dag Sunde

        #4
        Re: how to render an option value dynamic !

        "Dag Sunde" <ds@orion.no-way> wrote in message
        news:RoRIc.238$ Cc.70606@juliet t.dax.net...[color=blue]
        > "rf" <rf@.invalid> wrote in message
        > news:IwOIc.9214 4$sj4.39737@new s-server.bigpond. net.au...[color=green]
        > >
        > > "Gianni" <pincopallo_it@ yahoo.it> wrote in message
        > > news:3c03ef0c.0 407130145.6b4dd edd@posting.goo gle.com...[color=darkred]
        > > > I have to insert in a html select the last 10 years
        > > >
        > > > <select name="year" onChange="month ()" size=5>[/color][/color][/color]

        <snipped />
        [color=blue]
        > Here's one way to do it:
        >
        > Put <div id='tenYearsCom boLayer'></div> on the page where you want your
        > combo.
        >
        > then execute the following code in the page's onload() event:
        >
        > function drawTenYearComb o(){
        >
        > var comboLayer = document.getEle mentById('tenYe arsComboLayer') ;
        > var buffer = "<select name='year' onChange='month ();' size='5'>";
        >
        > var now = new Date();
        > var thisYear = now.getFullYear ();
        >
        > for( var n=10; n>0; n--)
        > buffer += "<option value='" + (thisYear - n) + "'>" + (thisYear - n) +
        > "</option>";
        >
        > buffer += "</select>";
        > comboLayer.inne rHTML = buffer;
        >[/color]

        err.... that for-loop should of course read:
        for( var n=10; n>=0; n--)

        --
        Dag
        58°26'15.9" N 008°46'45.5" E


        Comment

        • kaeli

          #5
          Re: how to render an option value dynamic !

          In article <3c03ef0c.04071 30145.6b4ddedd@ posting.google. com>,
          pincopallo_it@y ahoo.it enlightened us with...[color=blue]
          > I have to insert in a html select the last 10 years
          >
          > <select name="year" onChange="month ()" size=5>
          > <option value="1994">19 94</option>
          > <option value="1995">19 95</option>
          > <option value="1996">19 96</option>
          > <option value="1997">19 97</option>
          > <option value="1998">19 98</option>
          > <option value="1999">19 99</option>
          > <option value="2000">20 00</option>
          > <option value="2001">20 01</option>
          > <option value="2002">20 02</option>
          > <option value="2003">20 03</option>
          > <option value="2004">20 04</option>
          > </select>
          >
          > I did this way. I d like to do it dynamically in javascript but I have
          > no idea how to insert the result of a function in an option value.
          > Please help
          > Gianni
          >[/color]

          This sort of thing is best done with server-side scripting in case the
          client doesn't have javascript enabled. I have this very thing in JSP,
          though it's the last 5 years, this year, and the next 2.

          As to using javascript, you wouldn't insert the result of a function.
          You'd build the entire select element dynamically. Which, might I add,
          would not work in old browsers like NN4, either.

          Is this for an intranet application where you can know all browsers
          accessing your application? If not, you'd really be better off using a
          server-side script to dynamically build the html.

          If you still want to use javascript and having it work only in DOM
          browsers is okay by you, let me know and I'll post some code for you.

          --
          --
          ~kaeli~
          Murphy's Law #2000: If enough data is collected, anything
          may be proven by statistical methods.



          Comment

          • Mick White

            #6
            Re: how to render an option value dynamic !

            Gianni wrote:
            [color=blue]
            > I have to insert in a html select the last 10 years
            >
            > <select name="year" onChange="month ()" size=5>
            > <option value="1994">19 94</option>[/color]
            .....[color=blue]
            > <option value="2004">20 04</option>
            > </select>
            >
            > I did this way. I d like to do it dynamically in javascript but I have
            > no idea how to insert the result of a function in an option value.[/color]

            <form>
            <select name="year">
            </select>
            </form>

            <script type="text/JavaScript">
            function fillYearMenu(me nu,from,to){
            menu.length=0;z =to-from;
            for(s=from;s<=t o;s++){
            menu.options[menu.options.le ngth]=new Option(s+":
            "+(z-menu.options.le ngth)+" years ago",s);
            }
            }

            fillYearMenu(do cument.forms[0].year,1994,2004 )
            </script>


            Mick

            </script>

            The menu must exist for the function to work, and you'd need some error
            checking if your parameters will be provided by user entry.

            [color=blue]
            > Please help
            > Gianni[/color]

            Comment

            • Gianni

              #7
              Re: how to render an option value dynamic !

              kaeli <tiny_one@NOSPA M.comcast.net> wrote in message news:<MPG.1b5d9 d919a265fd7989f 7e@nntp.lucent. com>...[color=blue]
              > In article <3c03ef0c.04071 30145.6b4ddedd@ posting.google. com>,
              > pincopallo_it@y ahoo.it enlightened us with...[color=green]
              > > I have to insert in a html select the last 10 years
              > >
              > > <select name="year" onChange="month ()" size=5>
              > > <option value="1994">19 94</option>
              > > <option value="1995">19 95</option>
              > > <option value="1996">19 96</option>
              > > <option value="1997">19 97</option>
              > > <option value="1998">19 98</option>
              > > <option value="1999">19 99</option>
              > > <option value="2000">20 00</option>
              > > <option value="2001">20 01</option>
              > > <option value="2002">20 02</option>
              > > <option value="2003">20 03</option>
              > > <option value="2004">20 04</option>
              > > </select>
              > >
              > > I did this way. I d like to do it dynamically in javascript but I have
              > > no idea how to insert the result of a function in an option value.
              > > Please help
              > > Gianni
              > >[/color]
              >
              > This sort of thing is best done with server-side scripting in case the
              > client doesn't have javascript enabled. I have this very thing in JSP,
              > though it's the last 5 years, this year, and the next 2.
              >
              > As to using javascript, you wouldn't insert the result of a function.
              > You'd build the entire select element dynamically. Which, might I add,
              > would not work in old browsers like NN4, either.
              >
              > Is this for an intranet application where you can know all browsers
              > accessing your application? If not, you'd really be better off using a
              > server-side script to dynamically build the html.
              >
              > If you still want to use javascript and having it work only in DOM
              > browsers is okay by you, let me know and I'll post some code for you.
              >
              > --[/color]

              I d like to use javascript ... at the moment it seems easier !
              what else I have to do is to show the months of the year up to the
              corrent month. Can you help me in that too?
              Ciao
              Gianni

              Comment

              • kaeli

                #8
                Re: how to render an option value dynamic !

                In article <3c03ef0c.04071 40405.6cbb0a4b@ posting.google. com>,
                pincopallo_it@y ahoo.it enlightened us with...[color=blue][color=green]
                >>[/color]
                > I d like to use javascript ... at the moment it seems easier ![/color]

                That's debatable. :)
                I find it a lot easier in JSP personally.
                [color=blue]
                > what else I have to do is to show the months of the year up to the
                > corrent month. Can you help me in that too?[/color]

                Tested in NN7 and IE6 only. No assumptions should be made for other
                browsers - test in them if you want to check. Watch for word-wrappping.

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
                <html>
                <head>
                <title> New Document </title>
                <script type="text/javascript" language="javas cript">

                function createYearSelec t(parentElement , insertBeforeNod e)
                {
                if (! document.getEle mentById || !document.creat eElement)
                {
                alert("Sorry, your browser doesn't support this operation.");
                return;
                }

                var s = document.create Element("select ");
                s.id = "year";
                s.name = "year";
                parentElement.i nsertBefore(s, insertBeforeNod e);

                var today = new Date();
                var thisYear = today.getYear() ;
                if (thisYear < 2000) thisYear = thisYear + 1900;

                for (var i=10; i>=0; i--)
                {
                var o = document.create Element("option ");
                o.name = "o"+i;
                o.id = "o"+i;
                o.value = (thisYear-i);
                o.appendChild(d ocument.createT extNode(thisYea r-i));
                s.appendChild(o );
                }

                // attach onchange event to select
                if (s.attachEvent)
                {
                s.attachEvent(" onchange",editM onths);
                }
                else if (s.addEventList ener)
                {
                s.addEventListe ner("change",ed itMonths,false) ;
                }
                }

                function editMonths()
                {
                var monthArray = new Array
                ("January","Feb ruary","March", "April","May"," June","July","A ugust","Sept
                ember","October ","November","D ecember");
                // make months select depending on value of elementToCheck (year
                select element)
                // if it's already there, remove it and re-create

                frm = document.forms["f1"];
                elementToCheck = document.forms["f1"].elements["year"];

                if (! document.getEle mentById || !document.creat eElement)
                {
                alert("Sorry, your browser doesn't support this operation.");
                return;
                }

                if (document.getEl ementById("mont h"))
                frm.removeChild (document.getEl ementById("mont h"));

                // get the value of the first select
                var val = elementToCheck. options[elementToCheck. selectedIndex].value;

                // make month select element
                var s = document.create Element("select ");
                s.name = "month";
                s.id = "month";
                frm.insertBefor e(s, elementToCheck) ;

                // fill in month select element with all months IF the selected year
                is prior to this year
                // OR with the months up to this month if the current year is
                selected
                var L;

                var today = new Date();
                var thisYear = today.getYear() ;
                if (thisYear < 2000) thisYear = thisYear + 1900;
                if (val == thisYear) L = today.getMonth( );
                else L = 11;

                for (var i=0; i<=L; i++)
                {
                var o = document.create Element("option ");
                o.name = "o"+i;
                o.id = "o"+i;
                o.value = monthArray[i];
                o.appendChild(d ocument.createT extNode(monthAr ray[i]));
                s.appendChild(o );
                }
                }

                </script>
                </head>

                <body onLoad="createY earSelect(docum ent.forms['f1'], document.forms
                ['f1'].elements['sub']);editMonths(); ">
                <form id="f1" name="f1" action="" method="">
                <br>
                <input id="sub" name="sub" type="submit" value="submit">
                </form>
                </body>
                </html>

                --
                --
                ~kaeli~
                When you choke a smurf, what color does it turn?



                Comment

                • Dr John Stockton

                  #9
                  Re: how to render an option value dynamic !

                  JRS: In article <MPG.1b5ef771f6 cac8b9989f89@nn tp.lucent.com>, seen in
                  news:comp.lang. javascript, kaeli <tiny_one@NOSPA M.comcast.net> posted at
                  Wed, 14 Jul 2004 08:56:06 :[color=blue]
                  >
                  > var today = new Date();
                  > var thisYear = today.getYear() ;
                  > if (thisYear < 2000) thisYear = thisYear + 1900;[/color]

                  I have heard that there are browsers for which that will not work
                  correctly, since for 1997 to 2001 their getYear() gives 97,98,99,00,01 .

                  For mortals,
                  var thisYear = 2000 + today.getYear() %100
                  may thus be better.


                  The following is expected to be safe on any browser :

                  function getFY(D) { var YE
                  YE = Math.round(D.ge tTime() / 31556952000) + 1970
                  return YE + (D.getYear()-YE)%100 }

                  but needs testing. Note that the constants on the middle line do not
                  need to be exact, if I have reasoned correctly. The code returns the
                  last two figures of getYear(), as part of a number which is not far from
                  YE, and YE is a close approximation to the correct result.

                  See <URL:http://www.merlyn.demo n.co.uk/js-date1.htm#gY>
                  and <URL:http://www.merlyn.demo n.co.uk/js-date1.htm#gFY>

                  --
                  © 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

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: how to render an option value dynamic !

                    Gianni wrote:[color=blue]
                    > I d like to use javascript ... at the moment it seems easier ![/color]

                    You *can* use JavaScript, just do not use it client-side on a Web site.
                    Easier is not necessarily better, and the client-side solution is worse
                    here compared to the server-side one.


                    PointedEars

                    Comment

                    Working...