0

I'm using a bit of Mootools to access the values of an HTML select element but the thing is that the way to do it with Mootools [.getSelected()] returns an array and I don't know how to handle it.

my code :

<script type="text/javascript">
  window.addEvent('domready', function(){
    $('votconj').addEvent('click', function() {
      // This works great 
      $('jj_conjoint').addClass("validate['required']");
      $('mm_conjoint').addClass("validate['required']");
      $('aaaa_conjoint').addClass("validate['required']");
      $('conjoint_regime').addClass("validate['required']");
      new FormCheck('form');
    });
    if ($('nb_children').getSelected() == 1){
     // this doesn't work because .getSelected() returns an array and never equals 1
     $('jj_enfant1').addClass("validate['required']");
     $('mm_enfant1').addClass("validate['required']");
     $('aaaa_enfant1').addClass("validate['required']");
     new FormCheck('form');
    }
    if ($('nb_children').getSelected() == 2){
     // this doesn't work because .getSelected() returns an array and never equals 2 
     $('jj_enfant2').addClass("validate['required']");
     $('mm_enfant2').addClass("validate['required']");
     $('aaaa_enfant2').addClass("validate['required']");
     new FormCheck('form');   
    }

    new FormCheck('form');
}); 

</script>

3 Answers 3

1

getSelected() returns an array because some select elements allow multiple selection. If yours doesn't, you could just try $('nb_children').getSelected()[0]. To get the value you can use $('nb_children').getSelected()[0].get("value").

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but it doesn't work. I've updated my post to put a more comprehensive part of my code.
0

You can use .each to traverse an array in MooTools:

var selected = $('nb_children').getSelected();

selected.each(function(element) {
  var val = element.get('value');

  $('jj_enfant' + val).addClass("validate['required']");
  //etc
}

new FormCheck('form');

For more info: http://mootools.net/docs/core/Types/Array#Array:each

The reason why getSelected() returns an array at all times, is that code like this can always be reused when you decide to add multiple selectable items instead of just one.

Edit

Note that the above code is directly written. Might need some tweaking to get it working for your case.

Edit 2

Updated code to a more comprehensive example.

Comments

0

You want to check the value of the selected item, right?

Try with:

if ($('nb_children').getSelected().get('value') == 1){//...

1 Comment

Thanks for your answer but it doesn't work. I've updated my post to put a more comprehensive part of my code.

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.