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 6476595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:50:38+00:00 2026-05-25T06:50:38+00:00

My code is available below, and here: http://jsfiddle.net/fRpCy/ var input = []; input.push($(‘input’)); $(input).live(‘keydown’,

  • 0

My code is available below, and here: http://jsfiddle.net/fRpCy/

var input = [];
input.push($('input'));

$(input).live('keydown', function (event) {
    console.log('You have pressed a key!');
});

I would expect this code to respond to keypresses in the console. For some reason, it doesn’t. What is the problem with this code? (Note: I know how to fix it, I just don’t know what is wrong with it!)

  • 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-05-25T06:50:39+00:00Added an answer on May 25, 2026 at 6:50 am

    Your code is expecting the $() function to take an array of jQuery objects as an argument. You essentially have this:

    $([$('input')])
    

    Per this jQuery documentation, I don’t think it supports that. The jQuery function will take:

    • a selector string
    • an element
    • an element array
    • a jQuery object
    • some HTML
    • a function

    Important to note is that it will NOT take an array of jQuery objects. An element array is not the same thing as a jQuery object array, though you can obtain an element array from a jQuery object with the makeArray() method if you really wanted to.

    There are several alternatives that would work:

    The simplest is to not save any intermediary value:

    $('input').live('keydown', function (event) {
        console.log('You have pressed a key!');
    });
    

    This one gets an actual DOM element into the input array so the input array is a type of array that the jQuery function supports (though I can think of no reason to actually use this code):

    var input = [];
    input.push($('input').get(0));
    
    $(input).live('keydown', function (event) {
        console.log('You have pressed a key!');
    });
    

    Or, if there are multiple input values, just save the jQuery object for future use:

    var inputs = $('input');
    
    inputs.live('keydown', function (event) {
        console.log('You have pressed a key!');
    });
    

    Or, if you want an element array, you can get that like this:

    var input = $('input').makeArray();
    
    $(input).live('keydown', function (event) {
        console.log('You have pressed a key!');
    });
    

    Or, if what you really have is an array of jQuery objects and you want to combine those together into a new single jQuery object, this is one way to do that:

    // var input is an array of jQuery objects you already have
    
    var allElements = [];   // new empty array of DOM elements
    // iterate over array of jQuery objects getting array of DOM elements from each
    for (var i = 0; i < input.length; i++) {
        allElements = allElements.concat($.makeArray(input[i]));
    }
    $(allElements).live(...)
    

    Or, a bit simpler way to do it:

    var items = $.map(input, function(item, index) {return($.makeArray(item));});
    $(items).live(...)
    

    Or, now I’ve found the .add() method on a jQuery object:

    var $items = input[0];                      // grab first jQuery object in array
    for (var i = 1; i < input.length; i++) {
        $items.add(input[i]);                   // add other ones onto it
    }
    $items.keydown(function() {...});
    

    Incidentally, I’m finding the the array of DOM elements doesn’t work with .live(‘keydown’). I don’t know why. It works with:

    $(items).keydown(...)
    

    And, since you have an array of DOM elements that already exist, there is no reason to use .live() anyway. You can just use .keydown().

    jsFiddle working here: http://jsfiddle.net/jfriend00/qen2m/.

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

Sidebar

Related Questions

My code is available below and here: http://jsfiddle.net/CPLMu/51/ $timer = $('<input/>') .after($('<span class=ui-icon ui-icon-clock></span>'))
There’s a really cool diff class hosted by Google here: http://code.google.com/p/google-diff-match-patch/ I’ve used it
The test code is available here for your comments. 1) Rotation problem: I have
If you have a look here is the code below for future references. <h1>Accreditations</h1>
Is the source code available somewhere just to make a few small fixes to
I'm especially interested in solutions with source code available (Django independency is a plus,
Is there any code coverage tool available for PHP? I wish to check the
Which parsers are available for parsing C# code? I'm looking for a C# parser
Is there a freely available Base64 decoding code snippet in C++?
I have some code which I am making available via RMI. If my program

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.