Reputation: 17876
Here is my code
<form class="form-horizontal">
<div class="control-group">
<div class="controls">
<button onclick="javascript:add_user_group(2);" class="btn">add</button>
</div>
</div>
<table id="users-table" class="table table-striped" >
<thead>
<tr>
<th>User Name</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</form>
I just want to add a new row to a table when a add button clicked, but it seems a click of the add button triggers a submission of form.
Isn't is only a button with
type=submit
to submit form. Or any button in a form triggers a submission of form ?
Upvotes: 8
Views: 24328
Reputation: 1208
No need to use Javascript. Just set type="button"
to overwrite the implicit type="submit"
.
This can also be found in the HTML5 specs as mentioned here: https://stackoverflow.com/a/9643866/2175370
Upvotes: 53
Reputation: 573
Having false
returned in the function called onclick
will prevent form submission. :)
onclick="javascript:add_user_group(2);return false;"
Upvotes: 18