• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

JavaScript continuous Loop

Hi to all,

I am trying to loop through all the div tags on my page and the ones that contains "green" in the title I need to "wrap" in another div with a class called outer. This is for CSS effects. I am using the JQuery library, but I don't have enough experience with JavaScript to see why my query below goes into a continuous loop. I know it probably is because of the wrapping step, but I don't know how to avoid that.

Any suggestions on doing it better or how to stop the continuous loop.

Thanks to all
$(document).ready(function() {
            var divs = document.getElementsByTagName('div')
            for (var i = 0; i < divs.length; i++) {
                var weather = divs[i].title;
                //var style = divs[i].class;
                if (weather == "green") {
                    //alert("green div found");
                    $(divs[i]).wrap('<div class="outer"></div>');
                }
                else {
                    
                }                
            };
        });

Open in new window

0
ALawrence007
Asked:
ALawrence007
1 Solution
 
abelCommented:
I can't test your code and I'm surprised about the loop not ending, but here's a suggestion to try

$(document).ready(function() {
            var divs = document.getElementsByTagName('div')
            for (var i = 0; i < divs.length; i++) {
                if (divs[i].class != 'outer') {
                    var weather = divs[i].title;
                    //var style = divs[i].class;
                    if (weather == "green") {
                        //alert("green div found");
                        $(divs[i]).wrap('<div class="outer"></div>');
                    }
                    else {
                        
                    }                
                }
            };
        });

Open in new window

0
 
DanielWillmottCommented:
If you comment out the wrap line it doesn't loop continuously right?
0
 
HonorGodCommented:
Probably because you are messing with the document elements over which the loop is iterating.


0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
alien109Commented:
why not use the power of jquery? you can actually select just the divs with the title of green.

$(document).ready(function() {
    $("div[title='green']").wrap('<div class="outer"></div>');
}

I haven't tested that, but it should work.
0
 
ALawrence007Author Commented:
Daniel,

That is correct. When I take the wrap out it does not loop through the document.

HonorGod,

Your sample did not work, I got an error at the following:

if (divs[i].class != 'outer') {
                    var weather = divs[i].title;
                    //var style = divs[i].class;
                    if (weather == "green") {
                        //alert("green div found");
                        $(divs[i]).wrap('<div class="outer"></div>');
                    }

I am trying to follow the example below to obtain corners on my div tags.
http://www.methvin.com/jquery/jq-corner.html

Thanks Guys
0
 
abelCommented:
It wasn't HonorGod's example, it was mine. What's the error you got? I had to do it by heart, because I can't really mimic your situation without having the complete source.
0
 
abelCommented:
Btw, in the pasted code in your last comment, there's a missing closing accolade. Is that the cause of your error?
0
 
ALawrence007Author Commented:
Abel,

I am so sorry for the confusion. In my rush with this project I did not look correctly.

I missed Alien109's post in my rush and that was exactly what I was looking for.

Thanks to all and sorry for the confusion.
0
 
ALawrence007Author Commented:
Thanks Alien109
0
 
abelCommented:
that was a nice solution indeed!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now