iCyborg
iCyborg

Reputation: 4728

How to make People chose TextArea field (from multiple text area fields) in rails ?

so I just have one text field BODY filled with pre content First Value

<%= f.input :body, :input_html  => { :value => "First Value" } %>

Now I want to give the user the option to select a text area from multiple text area checkboxes, how can I do this ? See the image https://i.sstatic.net/PSGrR.png for better illustration

I am assuming there will be some jQuery involved

Thanks

Upvotes: 0

Views: 102

Answers (2)

dileep nandanam
dileep nandanam

Reputation: 2895

we can group the check box and corresponding input like this

<% form_for @object do |f| %>
    <div class="optional-input">
        <%= check_box_tag :attribute1 %>
        <%= f.text_area :attribute, :class => "display: none;" %>
    </div>
    <div class"optional-input">
        <%= check_box_tag :attribute2 %>
        <%= f.text_area :attribute2, :class => "display: none;" %>
    </div>
    ................
    ................

Now if we have the checkbox element, we can access the corresponding textarea since they are siblings. Then we can bind on-change event handler to each checkbox, which is child of div element.

$(document).ready(function(){
    $('.optional-input').find(':checkbox').change(function(){
        if($(this).is('checked')){
            $(this).siblings('textarea').show()
        }
        else{
            $(this).siblings('textarea').hide()
        }       
    })
})  

I hope it will work

Upvotes: 1

Kush Kella
Kush Kella

Reputation: 1213

Yes you are right you will have to use some jquery. Consider your checkbox id is "checkbox_1" and textarea id is "textarea_1", then you can make this happen as follows:

$(document).ready(function(e){
  $("#checkbox_1").click(function(e) {
    $("#textarea_1").focus();
  });
});

You can also put a check on the value of the checkbox if you want.

Upvotes: 1

Related Questions