Translate

Sunday, February 10, 2013

Button type submit vs button (HTML)

HTML
What it does?

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

<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="http://www.google.com/" method="get">
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <button onclick="DisplayHello()" type="button">
            Type Button</button>
        <br />
        <br />
        <button type="submit">
            Type Submit</button>
    </div>
</body>
</html>
<script type="text/javascript">
    function DisplayHello() {
        alert('Hello');
    }

</script>
</form>





No comments:

Post a Comment

Comments will appear once they have been approved by the moderator