Skip to content

Commit

Permalink
Make labels for nested radio buttons clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpace committed Aug 20, 2010
1 parent 37694d2 commit 6bfbec4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/helpers/form_helper.rb
Expand Up @@ -11,4 +11,8 @@ def errors_for(document)
end end
end end


def label_for_attr(builder, field)
(builder.object_name + field).gsub(/[\[\]]/,'_').squeeze('_')
end

end end
4 changes: 2 additions & 2 deletions app/views/apps/_fields.html.haml
Expand Up @@ -14,9 +14,9 @@
%div.watcher.nested %div.watcher.nested
%div.choose %div.choose
= w.radio_button :watcher_type, :user = w.radio_button :watcher_type, :user
= label_tag :watcher_type_user, 'User' = label_tag :watcher_type_user, 'User', :for => label_for_attr(w, 'watcher_type_user')
= w.radio_button :watcher_type, :email = w.radio_button :watcher_type, :email
= label_tag :watcher_type_email, 'Email Address' = label_tag :watcher_type_email, 'Email Address', :for => label_for_attr(w, 'watcher_type_email')
%div.user{:class => w.object.email.blank? ? 'choosen' : nil} %div.user{:class => w.object.email.blank? ? 'choosen' : nil}
= w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --' = w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --'
%div.email{:class => w.object.email.present? ? 'choosen' : nil} %div.email{:class => w.object.email.present? ? 'choosen' : nil}
Expand Down
9 changes: 7 additions & 2 deletions public/javascripts/form.js
Expand Up @@ -30,15 +30,20 @@ function makeNestedItemsDestroyable(wrapper) {
function appendNestedItem() { function appendNestedItem() {
var addLink = $(this); var addLink = $(this);
var nestedItem = addLink.parent().find('.nested').first().clone().show(); var nestedItem = addLink.parent().find('.nested').first().clone().show();
var timestamp = new Date();
timestamp = timestamp.valueOf();

nestedItem.find('input, select').each(function(){ nestedItem.find('input, select').each(function(){
var input = $(this); var input = $(this);
var timestamp = new Date();
timestamp = timestamp.valueOf();
input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
if(input.attr('type') != 'radio') if(input.attr('type') != 'radio')
input.val(''); input.val('');
}); });
nestedItem.find('label').each(function(){
var label = $(this);
label.attr('for', label.attr('for').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
});
addLink.before(nestedItem); addLink.before(nestedItem);
} }


Expand Down

0 comments on commit 6bfbec4

Please sign in to comment.