How can I trigger onclick function when I press enter button?
You might simply use $(document).keypress()
For this purpose extract your function out of .click()
to avoid code replication like this:
<input type="text" id="q" />
<input type="button" id="submit" value="submit" />
<script>
$(function () {
var executeFunction = function(){
var url = "/tag/";
url += $("#q").val();
window.location = url;
};
$("#submit").click(executeFunction);
$(document).keypress(function(event) {
if(event.which == 13) {
executeFunction();
}
});
});
</script>
Update
An even better solution would be the use of the jquery .submit()
event handler
<input type="text" id="q" />
<input type="button" id="submit" value="submit" />
<script>
$(function () {
$("#submit").submit(function() {
var url = "/tag/";
url += $("#q").val();
window.location = url;
});
});
</script>
Try with this:
$(document).keypress(function(e) {
if(e.which == 13) {
var url = "/tag/";
url += $('#q').val();
window.location = url;
}
});