0

I have the following form in my rails view:

<%= form_for :search do |f| %>
      <%= f.label :search_by_name %>
      <%= f.text_field :by_name  %>
      <%= f.submit 'Search' %>
<% end %>

In jQuery, I can do something like this:

        $("h1").click(function(){
            $("h1").hide();
        });

That would cause all h1 tags to become invisible upon clicking an h1 tag. How can I instead cause my entire form to become invisible by clicking on the "submit" button for my form above?

EDIT

Perhaps the issue is a little bigger than I'm thinking. When I do this per the answer below:

 $("form").submit(function(){
        $(this).hide();
    })

The form becomes invisible, but then the page is immediately reloaded and the form is visible again. It stays invisible for a split second before reloading the page. Is there a way to carry that affect over from page to page, or to display the form results without reloading the page?

Edit 2

Problem solved. I wrapped the form in a div class and passed that ID into the hide function as mentioned in the comments below, and that seems to work. I don't understand why the page was just realoading and displaying the form when passing in form, but it doesn't have that effect when passing in a id. But, problem solved.

3
  • What's the id of the form? Just use that in your hide function. Commented Dec 22, 2013 at 16:43
  • You want to lear about ajax, read something about remote forms stackoverflow.com/questions/6723334/… Commented Dec 22, 2013 at 16:52
  • Ajax, time to bite the bullet. Thanks. Commented Dec 22, 2013 at 17:01

2 Answers 2

1

Try

 $("form").submit(function(){
        $(this).hide();
    })
Sign up to request clarification or add additional context in comments.

Comments

0

Seems to me that Ajax is what you want (like @arieljuod said) but if you just wanted to hide the form without passing data to the server you could have prevented the event.

$("form").submit(function(e){
    e.preventDefault();
    $(this).hide();
})

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.