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
- var nameInputs = document.getElementsByName("name[]");
- for (var j=0; j<nameInputs.length; j++) {
- var nameInput = nameInputs[j];
- nameInput.onblur = function(value) {return function() {
- nameInput.onblur = "";
- }
- }(j);
- };