Thijs
Thijs

Reputation: 387

onChange event with rails3 javascript jQuery

I have a voting form where you can also add another answer, and then when you do that only then the voting button should appear...

EDIT 2!!!

In my views/index.html.erb i have:

<table>
<% @questions.each do |question| %>
<% for answer in question.answers %>


            <td>Other:</td>
            <td>        
              <%= form_tag('/vote/new_answer', :method => "post") do %>
              <%= hidden_field_tag('answer[question_id]', question.id) %>
              <%= hidden_field_tag('answer[user_id]', current_user.id) %>
            <div class="other_answer">
              <%= text_field_tag('answer[content]') %>
            </div>
          </td>
            <td>
                <div class="other_answer_button", hidden='true'>
              <%= submit_tag('Vote') %>
                </div>
<% end %>
<% end %>

On my application.js file i have:

$(function() {
    $(".other_answer input").change(function() {
        if ($.trim($(this).val()) == "") {
            $(".other_answer_button").hide();
        }
        else {
            $(".other_answer_button").show();
       }       
    });
});

Still the same remains:

Now it works... but if i put some text in one of the text fields all the vote buttons appear. And it only should show the one vote button at that specific answer.

Should i add somehow an incremental id to every text input field and also to the vote buttons?

Anyone? Regards, Thijs

Upvotes: 0

Views: 1037

Answers (1)

rubish
rubish

Reputation: 10907

Actually there are few other things as well which needs to be revised in your js code:

$(function() {
    $(".other_answer input").change(function() {
        if ($.trim($(this).val()) == "") {
            $("input", $(this).parents("td").next()).hide();
        }
        else {
            $("input", $(this).parents("td").next()).hide();
       }       
    });
});

The solution is very markup dependent and selector might not work as it is as I hove not tested, so try to play around with it a little, if it doesn't.

Upvotes: 1

Related Questions