tags:

views:

102

answers:

5

I am trying to change the value of the onblur attribute of a text input after the page has finished loading.

When I do the following, it simply fires the function:

ip.onblur = stopCalcUpInt(this,10);

When I do the following, I have success:

ip.onblur = function onblur(event){stopCalcUpInt(this,10);}

Unfortunately, the whole point of this is to be able to dynamically set the second parameter for stopCalcUpInt(). If I hard code a value for it... it works fine... but any attempts to pass varibles to this fails... instead of putting the value of the variable as the second param it just puts the plain text of the variable name itself. Here is ideally what I am TRYING to do:

ip.onblur = function onblur(event){stopCalcUpInt(this,this.value);}

In this example, when I alert the ip.onblur I get:

+2  A: 

It depends what this is intended to refer to. In an event handler this refers to the element on which the event is being handled. If that's what you want then your code looks good as written; this will point to ip.

If you intend this to refer to the this from outside the event handler and not ip then try this:

var self = this;
ip.onblur = function(event) { stopCalcUpInt(self, self.value); };
John Kugelman
'this' refers to the html input element that I am trying to change the onblur attribute of. The problem is that for some reason the phrase 'this.value' is being interpreted as a string instead of a reference to a variable.The epic frustration of all this is that I can hard code a number value and it works PERFECTLY.
DirtyBirdNJ
Your code is fine. As @Dancrumb says alerting `ip.onblur` prints out the source code for the function. Your problem must lie elsewhere. Try alerting in `stopCalcUpInt` and see what value gets passed in.
John Kugelman
+1  A: 
ip.onblur = function() {stopCalcUpInt(this,this.value);}
geowa4
removing the (event) argument to the function doesn't do anything differently as far as I can tell
DirtyBirdNJ
that's not the only difference. saying `ip.onblur = function onblur` is wrong. remove the second `onblur`. you have double-named the function.
geowa4
+1  A: 

ip.onblur is an event handler... i.e. it's a function

Now, when you alert a function, FF will show you the source code for that function (if it's user defined).

That is why you're seeing the plain text of the variable name.

For an event handler, this is the element that is currently handling the event. So, if you're setting the onblur handler of an input box, you will have access to the contents of that input box.

The code sample that you provided:

 ip.onblur = function onblur(event){stopCalcUpInt(this,this.value);}

should work correctly. Try

 ip.onblur = function onblur(event){alert(this.value); stopCalcUpInt(this,this.value);}

if you want to be sure

Dancrumb
The first time (before I make any changes) I alert the ip.onblur, the alert box shows the value in the HTML surrounded in quotes (for example "6"). Other functions that depend on this are able to use the "6" value properly. When I do the this.value assignment on it... those functions no longer work properly, making me believe something NaN or undefined is getting passed around. I really don't know... I'm very confused and frustrated.
DirtyBirdNJ
Have you tried my suggested code with the added alert... what is being alerted?
Dancrumb
A: 

Is stopCalcUpInt expecting a number in the second parameter? The value attribute will return a String, while in your hardcoded example you're passing a number type. Try this:

ip.onblur = function onblur(event){stopCalcUpInt(this,this.value * 1);}

As explained in QuirksMode:

Since multiplying assumes numbers, JavaScript makes the string a number, if possible.

Bob
A: 

The answer to getting this to work was super easy, yet not overly obvious. Instead of:

ip.onblur = function onblur(event){stopCalcUpInt(this,this.value);}

I did this:

ip.setAttribute('onblur','stopCalcUpInt(this,\'' + ip.value + '\');');

Works perfectly... no more banging my head against the wall! Yay!

DirtyBirdNJ
See my answer above, it should also work
Bob