Reputation: 455
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
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
Reputation: 1460
If your code is still not working you may try:
if(e.keyCode==13 || e.which == 13) // for cross browser
Upvotes: 0
Reputation: 5947
Try with this
$(document).keypress(function(e) {
if(e.keyCode == 13) {
alert('You pressed enter!');
}
});
Upvotes: 2