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

  • SEARCH
  • Home
  • 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 7718963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:17:46+00:00 2026-06-01T03:17:46+00:00

For the moment, we’re loading site-wide event-listeners from a single common.js file for a

  • 0

For the moment, we’re loading site-wide event-listeners from a single common.js file for a Rails project. We’re aware of (most of) the trade-offs involved there, and are just trying to mitigate them. Once our basic architecture takes hold, we may move them off to separate files by controller or by view.

For the moment, the quick question is how we can activate them only when necessary, which begs the mangled, pseudo-zen question:

if an event-listener is declared in a forest when nobody is around to hear it, does it still make a sound?

In other words, if one declares a basic listener (i.e., nothing persistent like .live() or .delegate()) in the JavaScript for a given page, and the target element is not actually present on that given page, does anything really happen, other than the few cycles devoted to evaluating it and checking the DOM for the element? Is it active in memory, looking for that element? Something else? It never seems to throw an error, which is interesting, given that in other contexts a call like that would generate a null/nil/invalid type of error.

For instance:

  $(document).ready(function () {
      $('#element').bind('blur keyup', function);
    }

Assume that #element isn’t present. Does anything really happen? Moreover is it any better to wrap it in a pre-filter like:

  $(document).ready(function () {
    if ($('#element')) {
      $('#element').bind('blur keyup', function);
    }

Or, are the .js interpreters in the browsers smart enough to simply ignore a basic listener declared on an element that’s not present at $(document).ready? Should we just declare the initial, simple form above, and leave it at that, or will checking for the element first somehow save us a few precious resources and/or avoid some hidden errors we’re not seeing? Or is there another angle I’m missing?

  • 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-01T03:17:48+00:00Added an answer on June 1, 2026 at 3:17 am

    JQuery was designed to work with 0+ selected elements.
    If no elements were selected, nothing will happen.

    Note that you will never get null when using jQuery selector. For example:

    $('#IDontExist') // != null
    $('#IDontExist').length === 0 // true (it's ajQuery object with 
                                  //       zero selected elements).
    

    The docs says:

    If no elements match the provided selector, the new jQuery object is “empty”; that is, it contains no elements and has .length property of 0.

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

Sidebar

Related Questions

At the moment I'm using bat file to launch my jar and set the
At the moment my application.js file looks like this: //= require_tree ./vendor //= require_directory
At the moment I have this: try{ // Create file FileWriter fstream = new
at the moment I have a small application and need to take information from
At the moment I have a class that requires information from another class, but
At the moment I have all my views specified in my mainWindow.xib file in
For the moment my batch file look like this: myprogram.exe param1 The program starts
At the moment I am working on a project admin application in C# 3.5
At the moment, I'm creating an XML file in Java and displaying it in
At the moment I'm working in a file with perhaps 1000 lines of code

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.