I am developing a module with form with lots of options, and some of them just have sense depending on the others.
The first approach was to make a multistep form, but now I am trying to use javascript to make some options appear/desapear depending on the other ones. I tryied to follow the example at http://drupal.org/node/189481 but I am really lost at how to add javascript to a form and how to access the form content.
My code is:

  drupal_add_js ("$(document).ready(
                    function() {
                      $('form-div-proves').change(
                        function() {
                          alert('canvi!!!!');
                        }
                      );
                    }
                  );");

  $form['proves'] = array(
    '#type' => 'select',
    '#title' => t('proves'),
    '#options' => array(1=>'a',2=>'b',3=>'c'),
    '#weigth' => -9,
  );

But what get is:

<script type="text/javascript" src="/ccc/$(document).ready(
                    function() {
                      $(& # 0 3 9 ;form-div-proves& # 0 3 9 ;).change(
                        function() {
                          alert(& # 0 3 9 ; canvi!!!!& # 0 3 9 ;);
                        }
                      );
                    }
                  );"></script>
.......
<div><div class="form-item">
 <label for="edit-proves">proves: </label>
 <select name="proves" class="form-select" id="edit-proves" ><option value="1">a</option><option value="2">b</option><option value="3">c</option></select>
</div>

I don't know why " or ' get translated to html, and how to access "proves" and depending of its value mark other fields as visible.

Any hint is wellcome, thanks

PD: I "spaced" & # 0 3 9 ; it appear joined in browser code.

Comments

depace’s picture

  drupal_add_js ("$(document).ready(
                    function() {
                      $('form-div-proves').change(
                        function() {
                          alert('canvi!!!!');
                        }
                      );
                    }
                  );", "inline");

  $form['proves'] = array(
    '#type' => 'select',
    '#title' => t('proves'),
    '#options' => array(1=>'a',2=>'b',3=>'c'),
    '#weigth' => -9,
  );
enboig’s picture

now " gets translated correctly, thanks.
I will "play" until I find how to access the form fields.

La vida és una taronja, què esperes per exprimir-la?

enboig’s picture

Now I get when the field is changed, but I don't know how to catch the value (in order to make un switch and just show the fiels I want...).
The code right now is:

  drupal_add_js ("$(document).ready(
                                  function() {
                                    $('#edit-proves').change(
                                      function() {
                                        //var opcio=$('#edit-proves').fieldValue();  //not working
                                        alert('jquery detectat!!!!');
                                        $('#edit-user').show();
                                      }
                                    );
                                    alert('hiding');
                                    $('#edit-user').hide(0);
                                  }
                              );", "inline");

La vida és una taronja, què esperes per exprimir-la?

enboig’s picture

I correct myself, the code is:

function wu_assign_user2($form_values = NULL) {
  $form=array();
  drupal_add_js ("$(document).ready(
                                  function() {
                                    $('#edit-proves').change(
                                      function() {
                                        var opcio=$('#edit-proves').val();
                                        alert('jquery detectat!!!! '+ opcio);
                                        $('#edit-user').show();
                                      }
                                    );
                                    alert('hiding');
                                    $('#edit-user').hide(0);
                                  }
                              );", "inline");

La vida és una taronja, què esperes per exprimir-la?