Sunday, February 10, 2013

Button type submit vs button (HTML)

What it does?

<button type="button">Click me!</button>

<button type="submit">Click me!</button>
Submits the page to the url set on form tag’s action attribute. Whether it does a get or a post is determined by the method attribute on the form tag.

 When to use <button type="button">?

When you want to execute a javascript function when a user clicks something that's when you use a <button type="button">.  You could also get away using a div tag or any other tag with an onclick event. But adding a button makes it more intuitive to the user. (Of course you could always style the div tag to look like a button)

Try it yourself

Copy paste this into a text file and name the file with a .html extension

<form action="" method="get">
    <meta name="viewport" content="width=device-width" />
        <button onclick="DisplayHello()" type="button">
            Type Button</button>
        <br />
        <br />
        <button type="submit">
            Type Submit</button>
<script type="text/javascript">
    function DisplayHello() {


No comments:

Post a Comment

Comments will appear once they have been approved by the moderator