0

I have to use Mootools for a website but being a real newbie I'm stuck with my code:

  var val = element.get('value');
  // Here I get a number between 1 and 6 and I'd like to implement a loop that goes from 1 to the value of val (between 1 and 6) 
  $('jj_enfant' + val).addClass("validate['required']");
  $('mm_enfant' + val).addClass("validate['required']");
  $('aaaa_enfant' + val).addClass("validate['required']");
  $('last_name_enfant' + val).addClass("validate['required','nodigit']");
  $('first_name_enfant' + val).addClass("validate['required','nodigit']");
1
  • 1
    MooTools is a JavaScript library. You should learn JavaScript before you go learning a JavaScript library. This is a very basic (fundamental) JavaScript question which you will easily find solutions to in any JavaScript tutorial. Commented Aug 22, 2011 at 10:59

2 Answers 2

1
var val = element.get('value').clean().toInt();

for (var ii = 1; ii <= val; ++ii) {
    $('jj_enfant' + ii).addClass("validate['required']");
    $('mm_enfant' + ii).addClass("validate['required']");
    $('aaaa_enfant' + ii).addClass("validate['required']");
    $('last_name_enfant' + ii).addClass("validate['required','nodigit']");
    $('first_name_enfant' + ii).addClass("validate['required','nodigit']");
}

// or... 

while(val--) {
    $('jj_enfant' + val).addClass("validate['required']");
    $('mm_enfant' + val).addClass("validate['required']");
    $('aaaa_enfant' + val).addClass("validate['required']");
    $('last_name_enfant' + val).addClass("validate['required','nodigit']");
    $('first_name_enfant' + val).addClass("validate['required','nodigit']");
}
Sign up to request clarification or add additional context in comments.

Comments

0

This isn't a Mootools question, just a javascript question.

for (var counter = 1; counter < val; counter++) {
    //Loop code
}

1 Comment

Teaching good JavaScript style from the start is a good idea. It's fairly universally accepted JavaScript style that function calls should be fn() and statements like for should be for () rather than for(), to differentiate them slightly more from function calls which they clearly are not. For these reasons have I edited your answer to insert the space.

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.