views:

77

answers:

5
 $('.dragbox').each(function(){
        $('.close').click(function(){
            $(this).parent().hide();
        }),
        $('.colpase').click(function(){
            $(this).siblings('.dragbox_content').toggle();
        })
     });    
+1  A: 

No. Presumably you want to apply the click handlers to matching elements within each dragbox. You can do that with:

 $('.dragbox').each(function(){
        $('.close', this).click(function(){
            $(this).parent().hide();
        }),
        $('.colpase', this).click(function(){
            $(this).siblings('.dragbox_content').toggle();
        })
     });    

If you just wanted to add the handlers globally, you wouldn't want the each.

Matthew Flaschen
+1  A: 

It doesn't look as if you need the each() function there. You may be applying the event handlers to the objects multiple times. Just:

    $('.close').click(function(){
        $(this).parent().hide();
    });
    $('.colpase').click(function(){
        $(this).siblings('.dragbox_content').toggle();
    });

Should do the trick.

Vincent Ramdhanie
asume if there are four boxes which i need to close. Thatswhy i used each function. if not how do we tel this close is for all of the close links in the page
amilanawarathna
When you say $('.close') you are actually getting a collection of all elements with the class "close". So doing this once will apply to as many close boxes as you have.
Vincent Ramdhanie
+3  A: 

Considering jquery works as a wrapped set (collection) i dont think you need the each method, just the

$('.dragbox').find('.close').click(function(){
    $(this).parent().hide();
})
$('.dragbox').find('.colpase').click(function(){
    $(this).siblings('.dragbox_content').toggle();
})

the handlers will be applied to all matched elements without the need for the each.

this will find all of the .close and .colpase inside of the .dragbox item(s) i assumed that is what you were after...

edited to use find in order to gain slight performance improvement. Thanks Dan/Alex.

Jason w
for readability sake, `$('.dragbox').find('.close').click(..` might be easier. (and from what i've read, although i don't have a article handy, a _touch_ faster)
Dan Heberden
@Dan Heberden - Brandon Aaron explains the heck out of contexts in JQ here: http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery (I don't know if it's changed significantly for 1.4).
Alex JL
well, if perf is a concern, you'll want to reuse `$('.dragbox')` instead of looking for it twice.
Isaac Cambron
@issac - Very True!
Jason w
well i wasn't getting too picky, though div.dragbox is the best you can get with classnames - although using a nearest id helps a bit too. i.e. `$('#mainSection div.dragbox')` - more-so it was the readability aspect - left to right kinda thing. @Alex - it has changed a in 1.4 - i think i saw/heard it on the 14 days of hawtness or yayquery.. but I don't even know if it's something you'd really notice speed wise unless you were doing hundreds of finds. And yes, always cache your jQuery selections if you're going to re-use them.
Dan Heberden
A: 

function set_colors(pick_color) { var color_code=pick_color; $('#'+ color_owner).parent().css('background-color',color_code); }

amilanawarathna
A: 

if you're referring to "parent" to actual ". dragbox" in "each"

$('.dragbox').each(function(){

  var self = this;

  $(self).find('.close').bind("click", function(){

    $(self).hide();

  }); // <-- ","?

  $(self).find('.colpase').bind("click", function(){

    //This line confuses me because you could do the same in the selector for the click event
    //unless you do have more things in the function
    $(this).siblings('.dragbox_content').toggle();

  });

  /*
  $(self).find('.colpase').siblings('.dragbox_content').bind("click", function(){
    $(this).toggle();
  });
  */

}); 
andres descalzo