dwashburn
dwashburn

Reputation: 37

JavaScript Do/While Loop Infinitely Looping

I know this is potentially a common question however I believe the logic and the code appear correct, yet it still seems to loop until the browser crashes.

$("#orderview").click(function(){
    do {
        $(".row1").clone().appendTo(".cardbox");
        var i = $( "listrow" ).length;
         }
    while (i < 10);
});

My function is creating Listrow class DIV's until there are 10 of them. My HTML starts with 1 DIV.

<div class="listrow news" id="row-a">
    <div class="l-padding floatleft">
        <div id="redditThumbnail"></div>
        <div class="articleheader news">
            <p class="mediatitle alignleft" id="redditTitle">
            </p>
            <p class="mediumtext floatleft alignleft">
                Submitted by
            </p>
            <div id="redditUsername"></div>
            <div class="half floatright">
                <p class="mediatext floatright s-color bold h-s-margin">
                    TEST
                </p>
                <p class="mediatext floatright p-color bold">
                    ACTION
                </p>
            </div>
        </div>
    </div>
</div>

The logic behind my attempted JS was:

  1. Clone the first DIV
  2. Check how many "Listrow" class DIVs are present
  3. If there are < 10 Listrow DIV's then restart the loop and clone another

Am I missing something? Thanks in advance for the assistance.

Upvotes: 0

Views: 46

Answers (1)

Ehsan
Ehsan

Reputation: 12959

change :

var i = $( "listrow" ).length;

to :

var i = $( ".listrow" ).length;

Upvotes: 3

Related Questions