user2443194 user2443194 - 7 months ago 24
Javascript Question

Creating button not calling any action inside form

Is there any way to create a button inside html form that wouldn't call an action specified in "Html.BeginForm"? I want to use it only for adding some elements to form by javascript. I have a different button that should call an action.

@using (Html.BeginForm...
{
...
<button id="addRow" class="btn margin-top-10 margin-bottom-10">addRowt</button>
...
}


The "addRow" button I'd like not to call any action.

Thanks in advance

Answer

Yes. You can listen to the click event of this button and prevent the default behavior. Assuming you ave jQuery library loaded in this page, you may use jquery preventDefault method to do this.

$(function(){

  $("#addRow").click(function(e){
     e.preventDefault();
     // do other things as needed (ex : add a row to ui)
  }); 

});