Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 17th, 2006, 07:45 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old June 18th, 2006, 08:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old June 19th, 2006, 05:10 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old June 19th, 2006, 05:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #5 (permalink)  
Old June 19th, 2006, 05:23 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Beg your pardon Joe, thanks for the help!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.