Skip to content
Advertisement

Populating form on keydown ENTER and preventing form from submitting

JS

$('.add_to_list').live('keydown',function (e){
    if(e.keyCode == '13'){
        var holder=$(this).attr('hold'),
            val=$(this).val();
            if(holder == 'mf' ||holder == 'mp'){
                    var v='#'+holder;   
                    h='<li class="entery deletable"><input type="hidden" name="'+holder+'[]" value="'+val+'">'+val+'</li>';
                    $(v).append(h);
                    $(this).val('');
            }
        e.prevent_default();    
        return false;
    }
    $('#save_clinic').submit(function(){return false;});
    });

HTML

<form accept-charset="utf-8" method="post" id="save_clinic" action="#">
<p>
<b>Findings</b>
<ol id='mf'></ol>
<input type="text" hold="mf" class="add_to_list" value="" name="">
<!--the input hv no name cause i dont want it to be submitted, this is for adding only-->
</p>
<p>
<b>Medical Procedures:</b>
<ol id=mp></ol>
<input type="text" hold="mp" class="add_to_list" value="" name="">
<!--the input hv no name cause i dont want it to be submitted, this is for adding only-->
</p>
<input type=submit>
</form>

Problem:
I want to prevent submit on keypress ENTER and only allow submit on submit button click, but my current js prevent submit for both and if I remove

$('#save_clinic').submit(function(){return false;});

from js then the form auto submit when user is trying to populate the form.

Can some one please tell me what is the problem here? example

Advertisement

Answer

The function is called preventDefault (no underscore, capital ‘D’), not prevent_default. Give that a try and remove this line:

$('#save_clinic').submit(function(){return false;});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement