Re: [jQuery] jQuery Plugin question
Create an empty object, pass it to the .each() block, and add each id as a property of it as you go. My first crack (which is probably hopelessly b0rked, but may get the point across...): var myInputs = {}; function sliderCallback { myInputs[this.id].rangeEl = $(this).siblings().filter(input.range); ...; }; $(.slider).each( sliderCallback( myInputs ) ); I hope I'm on the right track...? - Brian Is there a way to set a variable in an each that will be stored separately for each instance of the plugin (for example, if I am performing an action on all inputs, is there a way to store the input type and reuse it later?). Maybe I'm not being clear, so here's the example: $(.slider).each(function() { rangeEl = $(this).siblings().filter(input.range); rangeEl.css(border, 1px solid green); rangeMax = parseInt(rangeEl.attr(max)) || 100; rangeMin = parseInt(rangeEl.attr(min)) || 0; rangeVal = parseInt(rangeEl.val()) || 0; step = parseInt(rangeEl.attr(step)); difference = rangeMax - rangeMin; differential = difference / 100.0; $(this).Slider({ accept: .indicator, fractions: (difference / step), onSlide: function(cordx, cordy, x , y) { rangeEl.val(parseInt((cordx * differential) + rangeMin)) } }); $(this).SliderSetValues([[234 * ((rangeVal - rangeMin) / (difference)),0]]); rangeEl.val(rangeVal); }); What seems to be happening is that the rangeEl element is getting overwritten, and all sliders are using the same (last) element. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] jQuery Plugin question
Is there a way to set a variable in an each that will be stored separately for each instance of the plugin (for example, if I am performing an action on all inputs, is there a way to store the input type and reuse it later?). Maybe I'm not being clear, so here's the example: $(.slider).each(function() { rangeEl = $(this).siblings().filter(input.range); You haven't put the keyword var in front of rangeEl (and the others), so that creates a global variable; as you noted it's clobbered each time through the function. If you say var rangeEl it will be a local variable and you'll get a different one each time the function is called, creating a closure that your $(this).Slider() plugin can use. My experience is that 99 out of 100 times you want to say var anywhere you first start using the variable. Say it early and say it often--Javascript doesn't mind if you say it more than once in the same function. That especially applies to for-loop variables. Every for loop should start with for ( var or it's most likely broken in a very dangerous way. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] jQuery Plugin question
While I agree that var is often missing and global vars are often not a good idea... In JS vars are not scoped in a for or if so var inside the for (or if) does little more than a var before it. This is the reason that we have to resort to the confusing looking function(){var x = 1...}(); when the code says: var i = 22; for (var i = 0; i 5; ++i){}; alert(i); JS shows 5, any other normal language would show 22! Javascript doesn't have block scoping, providing only global and function scopes! On 10/4/06, Dave Methvin [EMAIL PROTECTED] wrote: Is there a way to set a variable in an each that will be stored separately for each instance of the plugin (for example, if I am performing an action on all inputs, is there a way to store the input type and reuse it later?). Maybe I'm not being clear, so here's the example: $(.slider).each(function() { rangeEl = $(this).siblings().filter(input.range); You haven't put the keyword var in front of rangeEl (and the others), so that creates a global variable; as you noted it's clobbered each time through the function. If you say var rangeEl it will be a local variable and you'll get a different one each time the function is called, creating a closure that your $(this).Slider() plugin can use. My experience is that 99 out of 100 times you want to say var anywhere you first start using the variable. Say it early and say it often--Javascript doesn't mind if you say it more than once in the same function. That especially applies to for-loop variables. Every for loop should start with for ( var or it's most likely broken in a very dangerous way. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒ ░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░ ▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒ ░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░ ▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] jQuery Plugin question
Also most JavaScript compressors depend on the var being their or you could get undesired results. -- Brandon Aaron On 10/4/06, Jörn Zaefferer [EMAIL PROTECTED] wrote: Ⓙⓐⓚⓔ schrieb: Javascript doesn't have block scoping, providing only global and function scopes! That may be right, but in most cases it is enough to keep your code clean to prevent problems resulting from the non-existant block scope. On the other hand, creating global variables causes quite a lot of trouble... -- Jörn ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/