ben
ben

Reputation: 6180

ruby on rails- how to add a text-area field by clicking a + button

I have this simple form having 5 questions(text-areas) for a user to fill. I want a user to be able to add extra questions by clicking a "plus" button.

What is the easiest way to do this? please I am not so strong in JavaScript including jQuery, so I would kindly ask for some clear code and if possible little explanation.

this is my views:

<% question_numbering = 0 %>
<%= simple_form_for(@quiz, html: {class: 'form-vertical', id: 'someform' }) do |f| %>

    <div>
        question <%= question_numbering += 1 %><br>
        <%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question."  %><br>
        question <%= question_numbering += 1 %><br>
        <%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question."  %><br>
        question <%= question_numbering += 1 %><br>
        <%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question."  %><br>
        question <%= question_numbering += 1 %><br>
        <%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question."  %><br>
        question <%= question_numbering += 1 %><br>
        <%= f.input_field :content, :placeholder => "enter your question."  %><br>
    </div>

    <button type="button" class="btn btn-default" id = "js-add-question-row">
        <span class="glyphicon glyphicon-plus"></span>
    </button>
    <%= f.submit 'Submit', :class => "btn btn-default" %>
<% end %>

pls NOTE the question <%= question_numbering += 1 %> is only for numbering the questions int the form.

Upvotes: 0

Views: 3172

Answers (1)

Related Questions