0

Hello I am using function

function prepareRadios(radioGroupName) {
  var radios = document.getElementsByName(radioGroupName);
  for( i = 0; i < radios.length; i++ ) {
    document.getElementById(radios[i].id).onchange = function() {
      radioUpdated(radioGroupName, radios[i].id); 
    };
  }
}

the problem is, that the onchange event fires with quote (radioGroupName, radios[i].id) instead of having those values put into it with my function

I need to pass the VALUES of them, not the names of the vars

What am I doing wrong?

1 Answer 1

2

It's closure-time :)

function prepareRadios(radioGroupName) {
    var radios = document.getElementsByName(radioGroupName);
    for( i = 0; i < radios.length; i++ ) {
        document.getElementById(radios[i].id).onchange = (function(name, id) {
            return function() { radioUpdated(name, id); }
        })(radioGroupName, radios[i].id);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.