By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

Hmm This wont take my i count !

P: n/a
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });

if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work
so frustrating, any ideas?

Aug 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sicapitan said the following on 8/29/2006 10:53 AM:
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });

if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work
so frustrating, any ideas?
Lots of ideas, but most people don't like my opinions of Prototype.js

But it is probably related to updateField not wanting a number but
wanting a String.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 29 '06 #2

P: n/a
sicapitan wrote:
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });
Is this the entire code, or is this part of a loop?
if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work
"Doesn't work"? What does it do? Lie on the couch all day?
so frustrating, any ideas?
Well, if that particular piece of code is part of a loop, your problems
are probably related to closures.

let's say your full code looks something like this:

function setup_observers () {
for (var i=0; i<10; i++) {
Event.observe('field'+i,
'blur',
function(e) {
updateField(i, "value", $F(this));
}
);
}
}

Then the function you send to observe() will contain a reference to the
variable i from the loop, not the value of the variable i. When i
changes in the for loop, so does i in all the functions you've just
created. So what basically happens is that the first argument to
updateField in this case, will be 10, which is the value of i when the
loop exits.

This is partly because it's expected closure behaviour, but it's also
because Javascript doesn't have block level scoping. The loop variable
is scoped within the entire function, not just the loop body.

The solution is to avoid the closure (at least within this block) and
create your observer function somewhere else.

function setup_observers () {
for (var i=0; i<10; i++) {
make_observer('field' + i,
'blur',
i,
'value');
}
}

function make_observer (element, effect, id, value) {
Event.observe(element,
effect,
function(e) {
updateField(id, value, $F(this));
}
);
}

(I don't know what "this" refers to in an Event.observe-callback, so I'm
not sure if it needs to be passed to make_observer as well)

--
Trond Michelsen
Aug 29 '06 #3

P: n/a
"sicapitan" <si*******@gmail.comwrites:
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });

if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work
I'm guessing this happens inside a loop, where "i" is incremented
until one after the last field's number.
Since there is only one _i variable, and all the functions refer to it,
they well all be using the value "'32'" if the last field is "field31".

This is
a) not a string containing a number, because of the extra "'"'s, and
b) not what you wanted (same, wrong, value used by all event handlers).

You need to create a new variable for each function, in order to have
different values, so try:

(function(_i) { // function creates new variable for each loop ...
Event.observe('field'+_i, 'blur', function(e) {
updateField(_i, "value", $F(this));
});
})(i); // ... when called immediately with the loop variable

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 29 '06 #4

P: n/a

sicapitan wrote:
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });

if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work
so frustrating, any ideas?
As far as I can tell, your problem is where you call the updateField
function. The way you did it, the first parameter is equal to "1" (when
i = 1, quotes included), so if you try to refer to the field "field1",
it actually refers to "field"1"" ... which doesn't exists (and is an
illegal field id too). Since you're using field1, field2, field3, etc..
Why enclosing i with quotes ? My guess i that you don't even need the
_i parameter.

Your watch function actually have an e(vent) parameter from whom you
can retrieve the element. Consider this :

// avoid anonymous functions...
var fieldObserver = function(e) {
// get the source element observed (in this case : "field"+i
// since IE doen't pass a value to e, we have to look also for
window.event
var element = Event.element( e || window.event );

// I don't know why you'd need the first parameter, but if you really
need the value for i,
// you could set it directly unto the element as $('field'+i).i = i
in the loop, and
// refer to it in here with element.i
updateField( element.id, "value", $F(element ) );
};

// hook the observer to the field
Event.observe('field'+i, 'blur', fieldObserver );
Good luck.

Aug 29 '06 #5

P: n/a
This helped a lot, thanks :) Looks like it will happen a lot for me,
hmmmm

Trond Michelsen wrote:
sicapitan wrote:
// add event
_i = "'"+i+"'";
/* Event.observe('field'+i, 'blur', function(e) {
updateField('"'+i+'"', "value", "$F(this)") });*/
Event.observe('field'+i, 'blur', function(e) { updateField("1",
"value", $F(this)) });

Is this the entire code, or is this part of a loop?
if i put a number in quotes "1" it works, if i put _i in there, it
doesnt work

"Doesn't work"? What does it do? Lie on the couch all day?
so frustrating, any ideas?

Well, if that particular piece of code is part of a loop, your problems
are probably related to closures.

let's say your full code looks something like this:

function setup_observers () {
for (var i=0; i<10; i++) {
Event.observe('field'+i,
'blur',
function(e) {
updateField(i, "value", $F(this));
}
);
}
}

Then the function you send to observe() will contain a reference to the
variable i from the loop, not the value of the variable i. When i
changes in the for loop, so does i in all the functions you've just
created. So what basically happens is that the first argument to
updateField in this case, will be 10, which is the value of i when the
loop exits.

This is partly because it's expected closure behaviour, but it's also
because Javascript doesn't have block level scoping. The loop variable
is scoped within the entire function, not just the loop body.

The solution is to avoid the closure (at least within this block) and
create your observer function somewhere else.

function setup_observers () {
for (var i=0; i<10; i++) {
make_observer('field' + i,
'blur',
i,
'value');
}
}

function make_observer (element, effect, id, value) {
Event.observe(element,
effect,
function(e) {
updateField(id, value, $F(this));
}
);
}

(I don't know what "this" refers to in an Event.observe-callback, so I'm
not sure if it needs to be passed to make_observer as well)

--
Trond Michelsen
Sep 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.