Projet

Général

Profil

Paste
Télécharger (1,95 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / pollfield / pollfield.js @ d719f12f

1
(function($) {
2
  // Re-enable form elements that are disabled for non-ajax situations.
3
  Drupal.behaviors.pollfield = {
4
    attach: function() {
5
      var field_name=$('input.pollfield-more-choice-button').attr('name');
6
      var number_of_choices = 0;
7
      $('.' + field_name + '-pollfield-choices').each(function(){
8
        number_of_choices++;
9
      });
10
      console.log(number_of_choices);
11
      $('input.pollfield-more-choice-button').click(function(event){
12
        var field_name=$(this).attr('name');
13

    
14
        var number_of_choices = 0;
15
        $('.' + field_name + '-pollfield-choices').each(function(){
16
          number_of_choices++;
17
        });
18

    
19
        var choice_id = new String;
20
        choice_id = field_name + '-0';
21
        var choice = $('#' + choice_id);
22
        var new_choice = choice.clone();
23

    
24
        new_choice.children().children().children('input').val('');
25
        new_choice.attr('id', field_name + '-' + number_of_choices);
26
        // Change input name.
27
        new_choice.children().children().children('input').each(function() {
28
          var name=String;
29
            name = $(this).attr('name');
30
            var new_name=name.replace('[group][0]', '[group][' + number_of_choices + ']');
31
            $(this).attr('name', new_name);
32
            var id = String;
33
            id = $(this).attr('id');
34
            var new_id = id.replace('group-0', 'group-' + number_of_choices);
35
            $(this).attr('id', new_id);
36
        })
37
        //change label for
38
        new_choice.children().children().children('label').each(function() {
39
          var for_name=String;
40
          for_name = $(this).attr('for');
41
            var new_for_name = for_name.replace('group-0', 'group-' + number_of_choices);
42
            $(this).attr('for', new_for_name);
43
        });
44

    
45
        number_of_choices++;
46
        new_choice.children().children().children('em').text(number_of_choices);
47
        $(this).before(new_choice);
48
        event.preventDefault();
49
      })
50
    }
51
  };
52
})(jQuery);