6

I have built a fairly complex form which includes a hidden section that the user can toggle open for entering more information if necessary. However, when you click on this toggle button labeled I have more Nativities, it triggers the submit button and prematurely submits the form.

The form is in dev right now, but it can be found here.

The code I am using for the toggle button is:

<script type="text/javascript">
 $(function() {
 $("#schedule").accordion({ header: "h5", collapsible: true });
 $("#more-nativities").hide();
 $("#toggle").click(function() {
 $("#more-nativities").slideToggle("slow");
 });
 });
 </script>

The code for the submit button is pretty basic:

<input id="submit2" type="image" src="_images/btn_register.jpg" name="submit" alt="" onmouseover="javascript:this.src='_images/btn_register2.jpg'" onmouseout="javascript:this.src='_images/btn_register.jpg'"/>

The code for the toggle button is:

<button id="toggle">I have more nativities</button>

Any ideas as to why the toggle button is triggering the submit? And more importantly how to solve the problem?

Thanks!

Peter Ajtai
57.8k13 gold badges124 silver badges143 bronze badges
asked Sep 19, 2010 at 22:30
2
  • 4
    As an aside, I would suggest not using inline JS. Write unobtrusive Javascript. Commented Sep 19, 2010 at 22:34
  • It's very helpful to provide as much pertinent code as possible. I edited in your original code for the toggle button. Commented Sep 19, 2010 at 22:46

4 Answers 4

12

Try adding a type, i.e.:

<button type="button" id="#toggle">Text</button>

http://www.w3schools.com/tags/tag_button.asp says this should be always defined. It's possible the browser is defaulting to a submit button.

answered Sep 19, 2010 at 22:36
Sign up to request clarification or add additional context in comments.

3 Comments

I don't know what you see but the button has no type.
Not seeing it on my browser. All I have is: <button id="toggle">I have more nativities</button>
@SimpleCoder - I didn't realize that was the code being referred to... I thought this was about the <input> button. The code for the <button> tags wasn't included in the OP. I've edited it into the question.
2

Esteban has one solution. A better one is described in the jQuery tutorial:

 $(document).ready(function(){
 $("a").click(function(event){
 alert("As you can see, the link no longer took you to jquery.com");
 event.preventDefault();
 });
 });
answered Sep 19, 2010 at 22:41

Comments

1

Try

 return false;

after the slide toggle on the click function fro the toggle button.

answered Sep 19, 2010 at 22:35

Comments

1

From W3Schools:

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".

http://www.w3schools.com/tags/tag_button.asp

Be sure to specify type="button"

answered Sep 19, 2010 at 22:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.