I am having a difficult time with keydown and stop propagation
i do not want my page to refresh, i have tried every which way i can think of, my current code is
<script>
$(document).ready(function() {
var e = jQuery.event( 'keydown', { which: $.ui.keyCode.ENTER } );
$('#id_number').trigger(e, function(event){
event.preventDefault();
event.stopPropagation();
});
});
</script>
any idea on what i am doing wrong here? I thought that the event was called correctly, i have jquery and jquery ui linked correctly and receive no console errors
UPDATE well it was working, now im getting Property 'event' of object function (a,b){return new e.fn.init(a,b,h)} is not a function error on the below code
$(document).ready(function() {
var e = jQuery.event( 'keydown', { which: $.ui.keyCode.ENTER } , function(event){
event.preventDefault();
event.stopPropagation();
});
$('#id_number').trigger(e);
});
UPDATE #2 - fixed
things I learned
rewrote it to this, works perfectly fine
$(document).ready(function() {
$('#id_number').keydown(OnKeyDown);
});
function OnKeyDown(e){
var code = (e.keyCode ? e.keyCode : e.which); //to support both methods
if(code == 13) { //the Enter keycode
//my actions
return false;
}
}
The code to stop the default action must go to the element that the event applies to..
$('#id_number').keydown(function(event){
event.preventDefault();
event.stopPropagation();
});
The .trigger()
second argument is for extraParameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With