SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop inside a for loop

    Hi,

    I have a for loop like below, In which i need to add another radio button which will be a loop according to a user selected value (Which is stored in a variable " i " ). How can i add another loop inside this ? or any other way to do this . Thanks in advance


    for (var NoPrticiField = 1; NoPrticiField <= selectPartiNo; NoPrticiField++) {
    participants.push({
    NoPrticiField: NoPrticiField,
    empname: ko.observable(),
    designation: ko.observable(),
    email: ko.observable()
    });
    }


    Eg :

    If i = 2

    My loop should be like below

    <input type="text" name="empname" id="empname">
    <input type="text" name="designation" id="designation">
    <input type="text" name="email" id="email">
    <input type="radio"> <input type="radio">

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,868
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you want to use another loop then simply store the loop counter for that loop in a different variable.

    Alternatively you could modernise your JavaScript code and get rid of the loops completely by converting the node list to an array and then using one of the modern methods that iterate over the entire array.

    ps the loop in the code you posted is using "NoPrticiField" for the loop counter and not "i" as you stated above the code.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi felgall,

    My loop should be something like below.

    for (var NoPrticiField = 1; NoPrticiField <= selectPartiNo; NoPrticiField++) {
    participants.push({
    NoPrticiField: NoPrticiField,
    empname: ko.observable(),
    designation: ko.observable(),
    email: ko.observable(),

    Here i need the new loop

    });
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi felgall,

    I have code like below, " option: ko.observable() " should be repeated "noOption" times...

    var participants = [];

    for (var NoPrticiField = 1; NoPrticiField <= selectPartiNo; NoPrticiField++) {


    fdsfs: for (x = 1; x <= i; x++) {

    noOption = 'Option: ' + x + ''

    alert(noOption);

    };

    participants.push({
    NoPrticiField: NoPrticiField,
    empname: ko.observable(),
    designation: ko.observable(),
    email: ko.observable(),
    option: ko.observable(),
    });
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •