0

Good day everyone!

I have a problem merging the codes in one function. (If it's possible).

First:

  • There is a table.
  • When the table row click two buttons will be enabled.

Here's the code:

   function enableRegButton() {
        $('#registerExist').prop('disabled', false);
        $('#edit').prop('disabled', false);

        // regButton execute when Enter key pressed
        $(document).unbind("keyup").keyup(function(e){ 
            var code = e.which; // recommended to use e.which, it's normalized across browsers
            if(code==13)
            {
                $("#registerExist").click();
            }
        });
    }

Second:

  • When Escape key pressed it will disable all bind buttons.

Here's the code:

   $(document).keyup(function(e) {
         if (e.keyCode == 27) { // escape key maps to keycode `27`
            $('#registerExist').prop('disabled', true);
            $('#edit').prop('disabled', true);
            document.getElementById("enStudID").value = "";
            document.getElementById("enInfoID").value = "";
            document.getElementById("enCoffID").value = "";
            document.getElementById("enYearID").value = "";
        }
    });

Now, what I want to do are those two codes above will merge in one function and it will call the function and trigger all those codes so when I edit the code it will be centralized.

Here's my final code:

function enableRegButton() {
        $('#registerExist').prop('disabled', false);
        $('#edit').prop('disabled', false);

        // regButton execute when Enter key pressed
        $(document).unbind("keyup").keyup(function(e){ 
            var code = e.which; // recommended to use e.which, it's normalized across browsers

            settings();

        });
    }
// This code is for ESC button when pressed.
$(document).keyup(function(e) {

         settings();
    });

function settings(){
        if(code==13)
        {
            $("#registerExist").click();
        }
        else if (code==27){ // escape key maps to keycode `27`
            $('#registerExist').prop('disabled', true);
            $('#edit').prop('disabled', true);
            document.getElementById("enStudID").value = "";
            document.getElementById("enInfoID").value = "";
            document.getElementById("enCoffID").value = "";
            document.getElementById("enYearID").value = "";
        }
    }

Problem:

  • Only the enable button is working when table row is click
  • Pressing Escape key will not disable the enabled buttons.
  • Code won't run when pressing Enter Key.
1
  • How many buttons do you have ? 2 by row, or globally two ? adding an html output example might be helpful. Commented Mar 30, 2017 at 6:54

2 Answers 2

3

You need to pass the key code to the settings method.

$(document).keyup(function(e) {
    settings(e.keyCode);
});

function settings(code) {

Use the browser's developer console when debugging Javascript issues, it's an invaluable tool and picks up problems like this quite easily.

Sign up to request clarification or add additional context in comments.

4 Comments

I'd recommend using e.which since they're already using jQuery, in order to avoid any cross-browser issues.
Thanks for the reply Daniel Bernsons it works! and also for Archer :-) btw off-topic I'm just confused with Javascript and JQuery differences when coding (lol) so I put the tag javascript.
@Daniel Bersons you mean the Inspect Element in chrome?
@Daniel Bersons oh! I see it... Thanks for that tip! Now it's easy to debugging javascript.
1

You are assigning the code variable in the unbind callback, not in the bind callback :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.