0

I am trying to replicate this HTML code

<input type="checkbox" value="" id="2" name="check" />
<label style="left: 0px;"for="2"><span>Name</span>

using this Ruby On Rails 4 code

<%= check_box_tag 'reason_ids[]', reason.id, false ,:required => true%>
<label style="left: 0px;"for="2"><span>Name</span>

So far I am getting the same visual appearance like HTML produces, but not the same functionality.

My goal: Click on label also checks checkbox.

At this point I can't check checkbox with click neither on checkbox or label. I have tried to change order of Rails code, but without success.

What I am doing wrong ? Thanks.

0

1 Answer 1

1

To achieve your goal:

Click on label also checks checkbox

You need to "connect" an input-label pair by id-for (input-id and label-for should have the same value):

<%= check_box_tag 'reason_ids[]', reason.id, false, :required => true, :id => reason.id %>
<label style="left: 0px;" for="2"><span>Name</span>

Note: added :id => reason.id to check_box_tag


Alternatively (different technique), you can wrap the check_box under a label (without the need to associate id-for):

<%= label_tag do %>
  <span>Name</span>
  <%= check_box_tag 'reason_ids[]', reason.id, false, :required => true %>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.