User1038
User1038

Reputation: 455

enter key press event is not working

This is Not working

HTML

<input type="text" class="form-control" id="newtopic" placeholder="Add Topic">  

Script

$(document).bind('keypress',function(){
$("#newtopic").keydown(function(e){ 
    if(e.keyCode==13){
        alert("Hello World");
    }
}); });

Upvotes: 0

Views: 5027

Answers (4)

Jai
Jai

Reputation: 74738

I think you have issues with your script stack order. you have to load jquery first then add your script below the specified library:

<script src="//code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function(){
        $("#newtopic").keydown(function(e){ 
           if(e.keyCode==13){
                alert("Hello World");
           }
        }); 
    });
</script>

Upvotes: 0

kp singh
kp singh

Reputation: 1460

If your code is still not working you may try:

if(e.keyCode==13 || e.which == 13) // for cross browser

Upvotes: 0

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Try with this

$(document).keypress(function(e) {
if(e.keyCode == 13) {
    alert('You pressed enter!');
}
});

Upvotes: 2

Amit
Amit

Reputation: 15387

Try this

$(document).ready(function(){
$("#newtopic").keypress(function(e){ 
    if(e.keyCode==13){
        alert("Hello World");
    }
 }); 
});

DEMO

Upvotes: 2

Related Questions