How can I call a already defined function on keypress?

stack

Here is my code:

    $(document).on('click', '.addNewName', function addNewUser() {
        var names = $('.compare_list td:nth-child(1)').map(function () {
            return $(this).text().trim();
        }).get();
        var newname = $('.newname').val();

        if ($.inArray(newname, names) != -1) {
            alert('this name is duplicate');
        } else if (newname == '') {
            alert('enter a name');
        } else {
            $('.compare_list > tbody:last-child').append('<tr><td>'+newname+'</td><td><i class="fa fa-times" aria-hidden="true"></i></td></tr>');
        }

    })

    // add user by Enter button
    $("#newnam").keypress(function (e) {
        if (e.keyCode == 13) {
            addNewUser();
        }
    });

When I click on .addNewName my code works as well. But when I press enter on keypress of #newname, it throws this error:

Uncaught ReferenceError: addNewUser is not defined(…)

Does anybody how can I fix it?

guest271314

At javasctipt at Question addNewUser is not defined outside of the scope of addNewUser function.

Define addNewUser as a named function, set as event handler at .addNewName click event, call and possibly pass parameters to addNewUser within #newnam keypress event.

   function addNewUser() {
      var names = $('.compare_list td:nth-child(1)').map(function() {
        return $(this).text().trim();
      }).get();
      var newname = $('.newname').val();

      if ($.inArray(newname, names) != -1) {
        alert('this name is duplicate');
      } else if (newname == '') {
        alert('enter a name');
      } else {
        $('.compare_list > tbody:last-child')
        .append('<tr><td>' + newname 
          + '</td><td><i class="fa fa-times" aria-hidden="true"></i></td></tr>');
      }

    }

    // add user by Enter button
    $(document).on("keypress", "#newnam", function(e) {
      if (e.keyCode == 13) {
       addNewUser() // set `this`, pass parameters to `addNewUser`
      }
    });

    $(document).on('click', '.addNewName', addNewUser);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I pass a keypress value to a function, through an event listener?

How can I simulate a keypress in JavaScript?

How I Can use a variable in a function which is defined in an another function?

How to solve libpng function already defined error?

How can I call a function with a dictionary?

Kotlin How can I call extension function

how could I modify/recompile an already defined function during runtime

How can I map a plugin-defined function call in a way that allows invocation with a count?

How can I call a function?

How can I call a function that is defined below where it is called?

In DB2 SQL, how can I kill a recursive function call that's already stuck in an infinite loop..?

How can I Execute/Call a user-space defined function from Linux kernel space module?

How can I call a function inside a class?

How can I call a function that is defined outside of a class from inside a class

How can I call function in PHP

How can I call a function that is defined in a different controller?

How can I call a function defined inside a module of requirejs from my page?

How can I apply a function call to an alias?

How can I call function?

How can i make a keypress action to this code?

How can I call this function in haskell?

Why can I call a function before it's defined?

How do I call a function that is defined inside of require() outside of the require()?

How can I call a defined function in python?

how can i mute an iframe on keypress

how do I assign a function to a declared function that has already been defined

Can't call function within keyDown or keyPress event handlers

How can I call a built-in function, if the function is already overrided in Python3

How can I ensure that a function is "defined" in Python?