programming GUI boxes via array?

Discussion in 'Javascript' started by Mark Scott, Feb 17, 2008.

  1. Mark Scott

    Mark Scott Guest

    I am trying to fill out some boxes on a webpage using JavaScript. Rather
    than use the wasy method (see attached function! is there anyway I can set
    the form element way by using a for loop? I tried:

    function showStockLevels()
    {
    var writeCounter
    for (writeCounter = 0; writeCounter < ProductPrices.length; writeCounter =
    writeCounter + 1);
    {
    document.administrator.itemCodes[writecounter]+MainStock.value =
    productPrices[writeCounter]
    }
    }

    but that threw an error. any ideas?

    <SCRIPT
    language="JavaScript"
    type="text/javascript">
    var productDescriptions = ['Superslurry electric blender', 'Apple - iPod
    (second-hand)', 'CoziNap nylon duvet tog 2', 'Headbanger mini hi-fi 20W',
    'MagiBoot shoe cleaning kit', 'The PushmiPulu lawnmower'];
    var productPrices = [45, 50, 15, 25, 75, 70];
    var productStock = [200, 0, 2, 500, 500, 15];
    var receivedLevels = [50, 10, 150, 500, 50, 100];

    function showStockLevels()
    {
    document.administrator.ssMainStock.value='200';
    document.administrator.aiMainStock.value='0';
    document.administrator.cnMainStock.value='2';
    document.administrator.hbMainStock.value='500';
    document.administrator.mbMainStock.value='50';
    document.administrator.ppMainStock.value='15'
    document.administrator.ssReceivedStock.value='50'
    document.administrator.aiReceivedStock.value='10'
    document.administrator.cnReceivedStock.value='150'
    document.administrator.hbReceivedStock.value='500'
    document.administrator.mbReceivedStock.value='50'
    document.administrator.ppReceivedStock.value='100'
    }

    </SCRIPT>

    </HEAD>

    <BODY>

    <FORM NAME = "administrator">
    <BR>
    SOFA SPEND plc. CURRENT STOCK LEVELS<BR><BR>
    Superslurry electric blenders <BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "ssMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "ssReceivedStock"
    VALUE = "">
    <BR><BR>
    Apple iPods (second hand) <BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "aiMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "aiReceivedStock"
    VALUE = "">
    <BR><BR>
    CoziNap duvets<BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "cnMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "cnReceivedStock"
    VALUE = "">
    <BR><BR>
    Headbanger hi-fi<BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "hbMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "hbReceivedStock"
    VALUE = "">
    <BR><BR>
    Magiboot cleaning kits<BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "mbMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "mbReceivedStock"
    VALUE = "">
    <BR><BR>
    PushmiPulu lawnmowers<BR> Current stock
    <BR>
    <INPUT TYPE = "text"
    NAME = "ppMainStock"
    VALUE = "" >
    Received:
    <INPUT TYPE = "text"
    NAME = "ppReceivedStock"
    VALUE = "">
    <BR><BR>
    <INPUT TYPE = "button"
    VALUE = "Show all stock holdings"
    ONCLICK = "showStockLevels()">
    </FORM>
    </BODY>
    </HTML>
    Mark Scott, Feb 17, 2008
    #1
    1. Advertising

  2. Mark Scott

    Doug Gunnoe Guest

    On Feb 17, 10:20 am, "Mark Scott" <> wrote:

    > function showStockLevels()
    > {
    > var writeCounter
    > for (writeCounter = 0; writeCounter < ProductPrices.length; writeCounter=
    > writeCounter + 1);
    > {
    > document.administrator.itemCodes[writecounter]+MainStock.value =
    > productPrices[writeCounter]


    That does not make sense.

    You are adding or concatenating 'itemCodes[writecounter]' and
    'MainStock.value' then assigning 'productPrices[writeCounter]' to
    what?

    If you are trying to modify all the input elements of your form, maybe
    something like this:

    var myForm = document.getElementsByName('administrator')[0];

    for(var i =0;i< myForm.childNodes.length;i++){
    if(myForm.childNodes.nodeName == "INPUT")
    myForm.childNodes.value = MainStock.value +
    productPrices[writeCounter] //or whatever you were trying to do here
    }

    and provided that MainStock.value is a legitimate reference
    Doug Gunnoe, Feb 17, 2008
    #2
    1. Advertising

  3. Mark Scott

    Mark Scott Guest

    THanks for that but im not sure what your code acheives (just getting into
    the javascript). What I was trying to do is use an array to reference
    each form element. I have created an array of the first bit of the form
    name and wanted to use a for loop to concatenate the first bit with the last
    bit.

    "Doug Gunnoe" <> wrote in message
    news:...
    On Feb 17, 10:20 am, "Mark Scott" <> wrote:

    > function showStockLevels()
    > {
    > var writeCounter
    > for (writeCounter = 0; writeCounter < ProductPrices.length; writeCounter =
    > writeCounter + 1);
    > {
    > document.administrator.itemCodes[writecounter]+MainStock.value =
    > productPrices[writeCounter]


    That does not make sense.

    You are adding or concatenating 'itemCodes[writecounter]' and
    'MainStock.value' then assigning 'productPrices[writeCounter]' to
    what?

    If you are trying to modify all the input elements of your form, maybe
    something like this:

    var myForm = document.getElementsByName('administrator')[0];

    for(var i =0;i< myForm.childNodes.length;i++){
    if(myForm.childNodes.nodeName == "INPUT")
    myForm.childNodes.value = MainStock.value +
    productPrices[writeCounter] //or whatever you were trying to do here
    }

    and provided that MainStock.value is a legitimate reference
    Mark Scott, Feb 17, 2008
    #3
  4. Mark Scott

    Doug Gunnoe Guest

    On Feb 17, 3:32 pm, "Mark Scott" <> wrote:
    > THanks for that but im not sure what your code acheives (just getting into
    > the javascript).


    My code references each input element of your form and modifies it.

    > What I was trying to do is use an array to reference
    > each form element. I have created an array of the first bit of the form
    > name and wanted to use a for loop to concatenate the first bit with the last
    > bit.


    Are you trying to change the values of the form?

    > document.administrator.itemCodes


    What is itemCodes? There is no itemCodes node in your document.

    Your statement is trying to access a node from the root 'document' to
    a node called 'administrator' and then a nonexistent child element of
    administrator called 'itemCodes'.

    A better way of referencing such nodes would be methods like
    getElementsByName, getElementByID.

    And can you explain what you expect the following to do?

    > document.administrator.itemCodes[writecounter]+MainStock.value = productPrices[writeCounter]


    Good luck.
    Doug Gunnoe, Feb 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul.foreman

    GUI - GUI value passing

    paul.foreman, Oct 22, 2004, in forum: Java
    Replies:
    5
    Views:
    734
    Michael Rauscher
    Oct 25, 2004
  2. Stefan Mueller
    Replies:
    5
    Views:
    12,347
    jamesxa
    Jun 16, 2009
  3. ulysses
    Replies:
    4
    Views:
    751
    Werner Schiendl
    Oct 22, 2003
  4. Dave Lilley
    Replies:
    22
    Views:
    259
    Marnen Laibow-Koser
    Jan 1, 2010
  5. Jai
    Replies:
    99
    Views:
    562
    Joshua Landau
    Dec 26, 2013
Loading...

Share This Page