473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using closure to pinpoint a form to empty itself

Hi,

I'm trying to use closure to pinpoint a form from a list of forms with the same name to erase its onblur function when the chosen form goes onblur.
For example, there will be indefinite numbers of text input forms with the same name "name[]" like this and same function "calculate()" when onblur,

name[] onblur= function () {calculate()};
name[] onblur= function () {calculate()};
name[] onblur= function () {calculate()};

And, when someone chooses any of the fields and goes onblur, it will intially execute a function, but right after that, onblur function will be deleted to prevent the same function being executed from the same form. So if I chose the second input field, the result would be,

name[] onblur= function () {calculate()};
name[] onblur= "";
name[] onblur= function () {calculate()};

So, I wrote this function employing the closure concept. However, for some reason, it nullifies the onblur function one by one from the top instead of just nullifying the chosen field.

What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. var nameInputs = document.getElementsByName("name[]");
  2. for (var j=0; j<nameInputs.length; j++) {
  3.     var nameInput = nameInputs[j];
  4.     nameInput.onblur = function(value) {return function() {
  5.     nameInput.onblur = "";
  6.     }
  7.     }(j);
  8. };
  9.  
Dec 23 '08 #1
1 1242
Dormilich
8,658 Expert Mod 8TB
in this case I recommend the use of removeEventListener() (works if you attached the event via addEventListener()) and I think that you have to define the event removal in the calculate() function.

something like*
Expand|Select|Wrap|Line Numbers
  1. function calculate()
  2. {
  3. // code comes here
  4. this.removeEventListener("blur", calculate, false);
  5. }
  6. // for each Input ...
  7. Input.addEventListener("blur", calculate, false);
regards

* some expert correct me if I'm wrong here
Dec 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
9
by: User1014 | last post by:
I'm a javascript noob and have a question concerning some code I've come across that I'm trying to understand. Here is a snippet of the relevant section: (snip) var closure = this; var xhr =...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
2
by: Silgd1 | last post by:
Hi all.... I'm using pyscripter 1.7.2, on a Win XP Prof 2002 - service pack 2 machine to script a website. I have no problem logging into the site, loading and an xml file, and retrieving the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.