user2759575
user2759575

Reputation: 553

Hide submit button before text is entered

Trying to simply hide the submit button before text is entered but nothing seems to be happening. (Something in the coffeescript is wrong but i don't know what - very new to js)

My form looks like this:

<%= f.input :body, as: :text, input_html: { :id => "inputBody" } %>
<%= f.submit "Answer", { :id => "button" }%>

Coffeescript looks like this:

$(document).ready ->
    $("#button").hide()
    $("#button").show() if $("#inputBody").length > 0

Upvotes: 1

Views: 240

Answers (1)

Jacek
Jacek

Reputation: 412

I do not know CoffeScript but in JavaScript You can try something like this:

$(function(){
     $("#button").hide();

     $("#inputBody").keyup(function() {
         var val = $(this).val();
         if (val.length > 0) {
             $('#button').show();
         }
         else {
             $('#button').hide();
         }

     });
});

Upvotes: 4

Related Questions