Commit 6bfbec4f098b569f8da5a670e47e23f27f41e9d2
1 parent
37694d23
Exists in
master
and in
1 other branch
Make labels for nested radio buttons clickable
Showing
3 changed files
with
13 additions
and
4 deletions
Show diff stats
app/helpers/form_helper.rb
@@ -11,4 +11,8 @@ module FormHelper | @@ -11,4 +11,8 @@ module FormHelper | ||
11 | end | 11 | end |
12 | end | 12 | end |
13 | 13 | ||
14 | + def label_for_attr(builder, field) | ||
15 | + (builder.object_name + field).gsub(/[\[\]]/,'_').squeeze('_') | ||
16 | + end | ||
17 | + | ||
14 | end | 18 | end |
15 | \ No newline at end of file | 19 | \ No newline at end of file |
app/views/apps/_fields.html.haml
@@ -14,9 +14,9 @@ | @@ -14,9 +14,9 @@ | ||
14 | %div.watcher.nested | 14 | %div.watcher.nested |
15 | %div.choose | 15 | %div.choose |
16 | = w.radio_button :watcher_type, :user | 16 | = w.radio_button :watcher_type, :user |
17 | - = label_tag :watcher_type_user, 'User' | 17 | + = label_tag :watcher_type_user, 'User', :for => label_for_attr(w, 'watcher_type_user') |
18 | = w.radio_button :watcher_type, :email | 18 | = w.radio_button :watcher_type, :email |
19 | - = label_tag :watcher_type_email, 'Email Address' | 19 | + = label_tag :watcher_type_email, 'Email Address', :for => label_for_attr(w, 'watcher_type_email') |
20 | %div.user{:class => w.object.email.blank? ? 'choosen' : nil} | 20 | %div.user{:class => w.object.email.blank? ? 'choosen' : nil} |
21 | = w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --' | 21 | = w.select :user_id, User.all.map{|u| [u.name,u.id.to_s]}, :include_blank => '-- Select a User --' |
22 | %div.email{:class => w.object.email.present? ? 'choosen' : nil} | 22 | %div.email{:class => w.object.email.present? ? 'choosen' : nil} |
public/javascripts/form.js
@@ -30,15 +30,20 @@ function makeNestedItemsDestroyable(wrapper) { | @@ -30,15 +30,20 @@ function makeNestedItemsDestroyable(wrapper) { | ||
30 | function appendNestedItem() { | 30 | function appendNestedItem() { |
31 | var addLink = $(this); | 31 | var addLink = $(this); |
32 | var nestedItem = addLink.parent().find('.nested').first().clone().show(); | 32 | var nestedItem = addLink.parent().find('.nested').first().clone().show(); |
33 | + var timestamp = new Date(); | ||
34 | + timestamp = timestamp.valueOf(); | ||
35 | + | ||
33 | nestedItem.find('input, select').each(function(){ | 36 | nestedItem.find('input, select').each(function(){ |
34 | var input = $(this); | 37 | var input = $(this); |
35 | - var timestamp = new Date(); | ||
36 | - timestamp = timestamp.valueOf(); | ||
37 | input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); | 38 | input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); |
38 | input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); | 39 | input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); |
39 | if(input.attr('type') != 'radio') | 40 | if(input.attr('type') != 'radio') |
40 | input.val(''); | 41 | input.val(''); |
41 | }); | 42 | }); |
43 | + nestedItem.find('label').each(function(){ | ||
44 | + var label = $(this); | ||
45 | + label.attr('for', label.attr('for').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2')); | ||
46 | + }); | ||
42 | addLink.before(nestedItem); | 47 | addLink.before(nestedItem); |
43 | } | 48 | } |
44 | 49 |