views:

52

answers:

4

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!

+1  A: 

Try

 return false;

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

Esteban Araya
+2  A: 

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.

ianhales
I don't know what you see but the button has no type.
SimpleCoder
Not seeing it on my browser. All I have is:<button id="toggle">I have more nativities</button>
ianhales
@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.
Peter Ajtai
+1  A: 

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"

SimpleCoder
+2  A: 

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();
   });
 });
Raffael Luthiger