0

I want to fade in an element with JQuery code when a inputfield is clicked. However, I cannot seem to get it to work, when the selector is placed inside the form. If I remove the selector outside the form it works fine. Is there any work-aroud for this? Or, maybe JQuery code does not execute inside a form? Here is my code:

<form action="newBet.jsp" method="get">

    <fieldset>          
        <input id="test" type="submit" name="update" value="&#9998;"/>
        <input class="form-control" id="div1" style="display:none;" type="number"/>
    </fieldset>

</form>


    $(document).ready(function(){
      $("#test").click(function(){
        $("#div1").fadeIn();
      });
    });

If I place the selector #test outside the form it works fine, but I want to execute the JQuery inside the form.

3
  • What do you mean by "removing selector outside the form"? Commented May 17, 2020 at 20:05
  • e.g. if I change the jquery selector #test to a button outside the <form></form> it works fine. Commented May 17, 2020 at 20:07
  • Please check out my answer :) Commented May 17, 2020 at 20:10

3 Answers 3

1

Your form gets submitted, which refreshes the page and doesn't let you see the result.

Instead of applying your event to the submit button, apply it to the form itself and return false to prevent default behavior (submitting):

<form id="test-form" action="newBet.jsp" method="get">
$(document).ready(function(){
    $("#test-form").submit(function(){
        $("#div1").fadeIn();
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank @Robo Robok. Is there a reason for choosing "return false" over prevent default from the other suggestions? I've tried both, and both works fine in this case.
Yes, they will do the same thing in modern browsers. Under the hood, jQuery calls event.preventDefault() and event.stopPropagation() when you return false in the callback. You should avoid using event directly in jQuery when not needed. jQuery is about abstraction. If you wanted to use event.preventDefault() yourself, I'd skip using jQuery entirely.
1
<input id="test" type="button" name="update" value="&#9998;" />

type="submit" refreshes the page. You neet to change type="button"

Comments

1

You can get the fadein effect with the input inside the form if you add preventDefault() like this:

 $(document).ready(function(){
      $("#test").click(function(e){
        e.preventDefault();
        $("#div1").fadeIn();
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="newBet.jsp" method="get">

  <fieldset>
    <input id="test" type="submit" name="update" value="&#9998;" />
    <input class="form-control" id="div1" style="display:none;" type="number" />
  </fieldset>

</form>

The default behaviour of the input of type "submit" inside the form is to submit the form which you can prevent using preventDefault() and call at a later time after the fadein effect.

3 Comments

Keep in mind that in jQuery it's cleaner to return false instead of doing e.preventDefault. jQuery was created with abstracting potential incompatibilities in mind and it does have an abstraction for preventDefault() and stopPropagation() in form of return false in the listener handler.
Thank you. That part about submit/refresh confused me. I've precented the default and called the submit upon the fadein.
@RoboRobok Thanks for your feedback, will keep it in mind in the future.

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.