Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 17th, 2006, 07:45 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default What am I doing wrong: Javascript For Loop

Hi all,

Here's the code:

<script language = "javascript">

    function addFillings(newId)
    {

    document.getElementById("fillsSpan").style.visibil ity = "visible"
    var newSel = document.createElement("SELECT")
    document.getElementById("fillsSpan").insertBefore( newSel)
    newSel.setAttribute('id', newId)
    newSel.setAttribute('size', 6)
    newSel.setAttribute('multiple', true)

        var fArray = new Array()
        fArray[0] = "cheese"
        fArray[1] = "ham"
        fArray[2] = "pickle"
        fArray[3] = "beef"
        fArray[4] = "turkey"
        fArray[5] = "tuna"


        for (i = 0; i<=5; i++)
        {
        var oAdd = fArray[i]
        document.getElementById(newId).options[i] = new Option(oAdd)
        }

    }

    function overSeeFills(noOfEls)//Number of sandwiches requested here = number of select boxes to create

    {
        for(i = noOfEls; i <= noOfEls; i++)
            var newId = document.getElementsByTagName("INPUT").length + Math.floor(Math.random() * 123)
                var newEl = new addFillings(newId)//create an id and pass it into my object, so I can reference the select box later }
</script>

I am trying to call my own object inside a for loop. The purpose is to create the same amount of select boxes as the user inputs for their menu choice. SO if the user wanted two paninis then I would offer two fillings select boxes, one for each panini. So what I've tried to do is pass the value of the input box into addfillings(noOfEls), in order to pass that into my loop and carry out that many instantiations of the creation of select boxes. (Hope that makes sense).

Help!

Cheers
Joe

__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
 
Old June 18th, 2006, 08:29 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You don't say what's going wrong but in your for loop try:
Code:
for (var i = 0; i < noOfEls; i++)
Also why not set the new listboxes' id to "lstFillings" + i rather than that complicated expression?

--

Joe (Microsoft MVP - XML)
 
Old June 19th, 2006, 05:10 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe. I did revise that part, to the number of select boxes on the page + 1. The problem I'm having is that my for loop only calls my custom object once and then exits. My object is the function called addFillings(newId). Its almost like it treats it as a normal function call. Maybe thats wjhat it IS doing and I've got the syntax wrong?

Thanks for your help
Joe

 
Old June 19th, 2006, 05:19 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Please re-read my answer. Your for loop only executes once because you start at i equal to number of elements and stop there as well.

--

Joe (Microsoft MVP - XML)
 
Old June 19th, 2006, 05:23 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Beg your pardon Joe, thanks for the help!






Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
incrementing javascript loop use i++ or ++i crmpicco Javascript How-To 4 May 11th, 2006 01:39 AM
how to build a JavaScript for loop decrement (--) crmpicco Javascript How-To 2 November 15th, 2005 12:24 PM
get value from javascript and use in php loop ghalebro XML 1 February 23rd, 2005 07:53 AM
Javascript .. wrong forum? ken1978 Pro JSP 0 July 19th, 2004 11:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.