0

I'm adding JQuery validation rules dinamically with the function below:

function addRules(rulesObj) {
    for (item in rulesObj) {
        $('#' + item).rules('add', rulesObj[item]);
    }
}

I'm using JQuery version 1.7.2 and JQuery validate 1.9 and it is not working in IE 7 and 8. Works fine in Chrome and FF. Am I missing anything? The error: Object doesn't support this action.

The rule looks like this:

var ctrxRules = {
        L4 : {
            required : true
             }
};

If the rule is empty it works.

6
  • I tried replicating this, but wasn't successful. Would you mind providing an example on fiddle.net that demonstrates this? Commented Jun 4, 2012 at 1:18
  • The answer below solved the problem. Commented Jun 4, 2012 at 17:43
  • I understand, however I'm curious how it solved the problem. Can you please provide an example of your faulty code via fiddle.net, so that we can better document the issue? As @Ryley said, he was unable to actually explain the reason why the code worked. Commented Jun 4, 2012 at 17:53
  • Well everything is on my computer at work. I didn't use any script debugger on IE but when I commented the addRules function or commented the body the page loaded. There is not much more I can say, I've stated the versions of JQuery, JQuery validate and IE Commented Jun 4, 2012 at 18:04
  • 1
    I would appreciate that. It's all part of furthering research and making StackOverflow a more helpful place. Thank you! Commented Jun 4, 2012 at 18:07

1 Answer 1

2

I can't explain why, but the solution is simple, add a var before item:

function addRules(rulesObj) {
    for (var item in rulesObj) {
        $('#' + item).rules('add', rulesObj[item]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's probably because declaring the variable without keyword 'var' pollutes the global scope with the 'item'

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.