This is a migrated thread and some comments may be shown as answers.

Button inside <form> submits form

2 Answers 1051 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robin
Top achievements
Rank 1
Robin asked on 17 Dec 2012, 05:52 PM
I have a button inside a <form> that always submits the entire form, even though it's not a submit button.

HTML:
<button id="button-app-preview" class="k-button">Oppdater</button>

JS:
$("#button-app-preview").click(preview);
function preview() {
    $("#app-preview").html(template({
        overskrift: $("#input-overskrift").val(),
        ingress: $("#input-ingress").val(),
        brodtekst: $("#input-brodtekst").val(),
        bildeurl: $("#bilde-sti").val()
    }));
}

Any ideas how come this button when clicked, first performs the preview() function, but then after it submits the entire form?

2 Answers, 1 is accepted

Sort by
0
Accepted
Holger
Top achievements
Rank 1
answered on 17 Dec 2012, 06:59 PM
Hi Robin,

you should explicitly set the button's type attribute to "button", otherwise it is handled as "submit".

<button id="button-app-preview" class="k-button" type="button">Oppdater</button>

Regards,
Holger
0
Robin
Top achievements
Rank 1
answered on 17 Dec 2012, 07:09 PM
Thanks a lot Holger, it was as easy as you described it.
Tags
General Discussions
Asked by
Robin
Top achievements
Rank 1
Answers by
Holger
Top achievements
Rank 1
Robin
Top achievements
Rank 1
Share this question
or