anatp_123
anatp_123

Reputation: 1205

How to get the button to work on keyboard Enter?

I'm trying to get the button to work without clicking on it.

I tried but with using 'Submit' and 'button', neither works unless user clicks the button.

 <input id="txtDusNumber" class="form-control form-control-sm" type="text" />
 <input type="submit" name="submit" class="btn " value="Search" id="enter-btn" onclick="ShowCard()" />
 <input type="button" class="btn " value="Search" onclick="ShowCard()" />


<script>
function ShowCard() {
        var dusNumberVal = $('#txtDusNumber').val();
        if (dusNumberVal) {
                $.ajax({
                        url: "@Url.Action("display","Card")",
                        data: { number: dusNumberVal },
                        type: 'GET',
                        success: function (result) {
                                $('#results').html(result);
                        },
                        error: function () {
                                alert("error...");
                        }
                });
        } else {
                alert('Please enter  Number.');
        }
}
</script>

Upvotes: 2

Views: 424

Answers (2)

colorswall
colorswall

Reputation: 188

Wrap <button type="submit"> in <form> tag

Upvotes: 1

Falcon
Falcon

Reputation: 857

Try this:

$(document).keypress(function(e) {
    if(e.which == 13) {
        ShowCard();
    }
});


function ShowCard(){
...
}

What it does is when the page is loaded, once you press enter, the ShowCard function gets executed.

Upvotes: 2

Related Questions