0
votes

I am using zend framework. I have added a new check box element for form. Example check box with values

abc xyz

When a checkbox is checked i want to append a textboxes next to each checkbox that is checked (after the close of label tag). I am trying to use jquery for this purpose. Can any one please help me on this issue.

1

1 Answers

0
votes

The following function adds a new text input after every checkbox when it is checked, and removes the text input when the checkbox is unchecked. The name of the text input is based on the name of the checkbox.

jQuery(function($){
  var key = 'checkbox-master';
  $('input:checkbox').click(function(){
    var self = $(this);
    var next = self.next();
    if (this.checked){
      if (next.data(key)!=this){
        var n = this.name+'-text';
        self.after($('<input type="text" name="'+n+'">').data(key,this););
      }
    }else{
      if (next.data(key)==this) next.remove();
    }
  });
});

I wonder if you really want this, however, instead of just dynamically showing and hiding some pre-created text inputs that have the names you want. That would be cleaner, assuming the text inputs are always immediately following the checkboxes in code:

jQuery(function($){
  $('input:checkbox').click(function(){
    $(this).next()[this.checked ? 'show' : 'hide']();
  });
});