views:

23

answers:

2

I have a form, with a submit button:

<% form_for(@personalentry) do |f| %>
    <%= f.submit 'Create' %>
<% end %>

HTML Output

<form action="/personalentries" class="new_personalentry" id="new_personalentry" method="post">
    <input id="personalentry_submit" name="commit" type="submit" value="Create" />
</form>

I want to add a second button, to do some other code not related to form submitting. So I did this:

<head>
    <script type="text/javascript">
        function addEndDateCalendar(){
            alert("test");
        }
    </script>
</head>
<body>
    <form action="/personalentries" class="new_personalentry" id="new_personalentry" method="post">
        <button name="addEndDateButton" onClick="addEndDateCalendar()">End date?</button>
        <input id="personalentry_submit" name="commit" type="submit" value="Create" />
    </form>
</body>

When I click the button, the alert comes up, but then it runs the submit code. How can I make this second button not run the submit code after it runs its own code? Thanks for reading.

+3  A: 

You could try using type="button" instead:

<input type="button" name="addEndDateButton" onClick="addEndDateCalendar()" value="End date?" />
roryf
A: 

If your function addEndDateCalendar() return false, then the submit shouldn't occur.

Clement Herreman