[jQuery] Re: Fighting a closure

2008-01-31 Thread Charles K. Clarkson
timothytoe wrote: : This next one is obviously a work of JavaScript art (as opposed to the : former, which is a more jQuery solution). Actually, it's very jQuery-ish. We can use a similar form to add custom methods to the jQuery object. : Does anyone want to explain it? : : for ( i=0;i<

[jQuery] Re: Fighting a closure

2008-01-31 Thread timothytoe
Ah, that makes sense. Thanks. On Jan 31, 4:04 pm, Danny <[EMAIL PROTECTED]> wrote: > Depends on what you're trying to do. In your case, the index and the > value are identical, but next time they may not be: > $.each( ['a','b','c'], function (index, val){ > // index is 0...1...2, val is 'a'...'

[jQuery] Re: Fighting a closure

2008-01-31 Thread Danny
Depends on what you're trying to do. In your case, the index and the value are identical, but next time they may not be: $.each( ['a','b','c'], function (index, val){ // index is 0...1...2, val is 'a'...'b'...'c' }); On Jan 31, 3:40 pm, timothytoe <[EMAIL PROTECTED]> wrote: > $.each( [0,1,2,3,4

[jQuery] Re: Fighting a closure

2008-01-31 Thread timothytoe
$.each( [0,1,2,3,4], function(index, num) { $("#port"+num).click(function() { bigchart(num); }); }); Wait. What's the "index" in there for. I don't need that, do I? Seems to work fine without it. I'll assume that was a leftover from a previous idea.

[jQuery] Re: Fighting a closure

2008-01-31 Thread timothytoe
I ended up using this. I liked it much better than my previous solution (passing in "this" and then stripping the digit out in the handler). $.each( [0,1,2,3,4], function(index, num) { $("#port"+num).click(function() { bigchart(num); }); }); This one is easy for me to read, comprehend, and m

[jQuery] Re: Fighting a closure

2008-01-31 Thread Josh Nathanson
for ( i=0;i<5;i++ ) { (function(num) { $("#port"+num).click(function() { bigchart(num) }); })(i); } That's the one I was trying to figure out. Nicely done. -- Josh

[jQuery] Re: Fighting a closure

2008-01-31 Thread timothytoe
Thanks for all the examples. I was able to find a couple ways on my own, but I love to see how other people solve these problems so I can learn new techniques. Hardest thing for me when in JavaScript is taking my mind out of C and PHP. Shared syntax is a blessing and a curse.

[jQuery] Re: Fighting a closure

2008-01-31 Thread Brandon Aaron
One more reply for good measure ... Using jQuery.each will provide you the isolation you need to make your original code work. jQurey.each( [0,1,2,3,4], function(index, num) { $("#port"+num).click(function() { bigchart(num); }); }); You should also be able to write it like this: for ( i=0;

[jQuery] Re: Fighting a closure

2008-01-30 Thread Karl Swedberg
On Jan 30, 2008, at 9:13 PM, Karl Swedberg wrote: If you can assign class="port" to each of those elements, you could do this: $('.port').each(function(index) { $(this).click(function() {bigchart(index)}); } if you can't assign a class, then change $('.port') to $ ('[id^=port]') ,

[jQuery] Re: Fighting a closure

2008-01-30 Thread Ariel Flesler
The only thing you need to do is to capture the number on a scope. Like this: function bind( num ){ $('#port'+num).click(function(){ bigchart(num); }); }; for( var i=0; i<5; i++ ) bind( i ); And voila, you can also do: function getHandler( num ){ return function(){ bigchart(num); });

[jQuery] Re: Fighting a closure

2008-01-30 Thread Karl Swedberg
On Jan 30, 2008, at 3:10 PM, timothytoe wrote: I think I submitted a half-done version of this message by accident a few minutes ago. Sorry. This works: $("#port0").click(function() {bigchart(0)}); $("#port1").click(function() {bigchart(1)}); $("#port2").click(function() {bigchart(2)}); $

[jQuery] Re: Fighting a closure

2008-01-30 Thread Feijó
I'm yet to be an expert in jQuery :) But I would solve that like this: $(".port").click(function() {bigchart($(this).attr('port-id') )}); now in your html code, I guess its like: change to Feijó timothytoe escreveu: I think I submitted a half-done version of this message by ac

[jQuery] Re: Fighting a closure

2008-01-30 Thread Josh Nathanson
There is a clever way -- in John Resig's book he talks about it -- it involves executing the anonymous function right after declaring it, when in a for loop: function() { do whatever; }(); But, I tried some experiments and I couldn't get it to work right. -- Josh I ended up doing someth

[jQuery] Re: Fighting a closure

2008-01-30 Thread timothytoe
I ended up doing something similar to what you suggest, but I really was hoping to elicit some clever way to get the value of the variable at the time of the assignment, rather than the "5" that I get because the closure is maintaining the context. Is there a way to set these event handlers up in

[jQuery] Re: Fighting a closure

2008-01-30 Thread Josh Nathanson
Instead of binding five times, you can do it dynamically: $("[id^=port]").click(function() { bigchart( this.id.charAt(this.id.length-1) ); }); Something close to that should do it. Although if you end up with 10 or more clickables you'll have to change your naming scheme a bit. -- Jo