SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: tricky for loop

  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tricky for loop

    Code:
    	clickListener: function()
    	{
    		var containerRow = this.parentNode.parentNode;
    		
    		for (var el = containerRow.nextSibling; el; el = el.nextSibling)
    		{
    			if (el.nodeType == 1) 
    			{
    				if (el.cells[0].className == "container")
    				{
    					break
    				}
    				
    				if (!Core.hasClass(el, "hide"))
    				{
                                            Accordion.storeCookie(containerRow.id);
    
    					Core.addClass(el, "hide");
    				}
    				else
    				{
    					Core.removeClass(el, "hide");
    				}
    			}
    		 }
    	}
    I want to run the statement: "Accordion.storeCookie(containerRow.id);" once. Is possible to do this in for loop ? Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    clickListener: function()
    {
    var containerRow = this.parentNode.parentNode;
    Accordion.storeCookie(containerRow.id);"
    // continue coding...
    Proud member of this community.
    ____________________________

  3. #3
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, is not hard to put the statement outside for loop. But I need to run the statement, if element not has class.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this

    Code:
    var cookieSet = false;
    
    for loop...
        if (!cookieSet) {
             // set the cookie
            cookieSet = true;
        }
    end of for loop

  5. #5
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks your example work great.
    Now I ask a question, Is not possible for me to call the store cookie function in clickListener method and call a separate method to collapse/expand the elements? Something like:
    Code:
    clickListener: function()
    {
        storeCookie ....
        expand ......
    },
    
    expand: function()
    {
        // the for loop
    }
    Is not more elegant? Sorry this is my logic.. Im beginner


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
  •