Search code examples
jqueryformsbuttoncheckboxenter

if checkbox is checked press enter to trigger button click


I have a form which shows 1 question at a time and i have to click next in order to go to the next question. The user have to select one option to move to the other questions. I want to do is if the user selects a option from the checkbox and press enter it will trigger the next button and move to the other questions. i cant possibly do it. what is wrong with my code? jquery code..

$('input[name="trading_for_at_least_6_months"]:checked').keypress(function (event) {
if (event.keyCode === 13) {
    $(".comnext1").click();
}});

I am trying to code is to see if any of the options is checked by checking the input names and if it is, when enter key is pressed trigger next button. I have other next button so i dont want to trigger them too.


Solution

  • You can use it something like this:

    <script>
    $(document).ready(function() {
       $(document).on('keypress', function(event) {
        if($('input[name="trading_for_at_least_6_months"]').is(':checked')) {
            if (event.keyCode === 13) {
                alert(1);
                $('.comnext1').trigger('click');
                return false;
            }
        }
       });
    });
    </script>
    

    Remove the alert in above code it's for testing that event is working on submit and button is getting clicked.