Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:42:42+00:00 2026-06-18T02:42:42+00:00

This seems very simple, but for some reason it’s not working as expected. I

  • 0

This seems very simple, but for some reason it’s not working as expected.

I am trying to make a very simple jQuery extension/plugin which allows me to simply reduce my lines of code when requiring a trigger on an enter key (and a similar for an escape)

Here’s my code:

$.fn.enterListen = function (callBack) {
    $(this).on('keyup', function (e) {
        if (e.keyCode == 13) {
            callBack;
            // $(this).enterListen(callBack); // trying to rebind...
        };
    })
};

Then when an element is dynamically created with jquery we might do something like:

var input = $('<input'>).enterListen(function (){ alert("Enter was pressed"); });
$(input).appendTo('body');

Now we’ve added an input element to the page, in which we can type and when enter is pressed it triggers the alert.

This works, except, only once.

You can see a commented out line in my code above where I am trying to rebind the function the the element after the enter trigger is activated, and even that doesn’t make it work a second time.

You can press as many other keys as you like before pressing Enter, but as soon as you do, it seems to unbind the keyup event.

IF… however, I run it like this:

function isEnter(e, ele) {
    if ((e * 1) == 13) {
        $(ele).click();
    };
};

Called by:

var input = $('<input'>).on('keyup', function (e) { isEnter(e.keyCode, $(ok)) });
$(input).appendTo('body');

It works fine, but to me it is clumsier in the code, I am trying to create a library of extensions to make the inner coding of this project a bit shorter… perhaps I am just putting too much time into something I needn’t…

Anyway, if anyone could shed any light on why the event becomes unbound, that’d be lovely!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:42:44+00:00Added an answer on June 18, 2026 at 2:42 am

    Inside a jQuery plugin, this is the jQuery object, no need to rewrap it. e.which is normalized in jQuery. To execute a function you need parenthesis (). And most importantly, you need to return this otherwise the input variable will be undefined, and if you intend to do stuff inside your plugin with selectors containing multiple elements, you need to return this.each(function() { ... }) etc. as explained in the plugin authoring documentation from jQuery.

    $.fn.enterListen = function (callBack) {
        return this.each(function() {
            $(this).on('keyup', function (e) {
                if (e.which == 13) {
                    e.preventDefault();
                    callBack();
                }
            });
        });
    };
    
    var input = $('<input />').enterListen(function (){ 
        alert("Enter was pressed"); 
    });
    
    input.appendTo('body');
    

    FIDDLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a very simple problem, but for some reason I find myself
I wrote this very simple Ember code, but for some reason the event doesn't
On the face of it, this seems a very simple problem but for some
I feel like this must be very simple, but for some reason I am
This seems like it should be very simple but I can't get it to
This problem seems very simple to me, but I've been unable to fix it,
I'm trying to do something that seems like it should be very simple, but
I'm sure this is a very simple fix but I cannot seem to find
This must be very simple. But I just can't seem to find the proper
This is a very simple question, but I can't seem to find something about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.