Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery events on non-present elements

Tags:

jquery

events

Is it bad to tie events to ids that aren't on the page in certain circumstances (based on PHP)? Will it cause code errors and slowness, or will jQuery automatically ignore them?

Should I instead put something like the following on every element that might not be there?

if ( $('#element') ){
    $('#element').click(function(event) {}
}

My page setup is a certain edit box is not loaded under certain conditions to keep functions from being tried.. I set it up with a conditional php include because it's based on circumstances in the database that I don't really want to pass through to the front-end.

Right now all the js for the on page is in a single file with a number of functions.

like image 717
Damon Avatar asked Sep 21 '11 15:09

Damon


People also ask

Which jQuery event occurs when add one or more event handlers on the specific child element of matching elements?

delegate() Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

Why is jQuery used to wire up events?

jQuery makes it straightforward to set up event-driven responses on page elements. These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved.

Which jQuery method triggers or binds a function to the error event of selected elements?

jQuery error() Method The error() method triggers the error event, or attaches a function to run when an error event occurs.


2 Answers

When jQuery selector doesn't return any elements, no events are being bound. So you're making no harm by not writing if statement.

This means that internally jQuery binds event handler to all matched elements. When there are none, no handler will get bound to any element. This means: you don't have to check element existence in your code.

Future reference

Whenever you're doing

if ( $('#element') ){
    $('#element').click(function(event) { /* blah blah */ });
}

you should rather save selector results

var result = $('#element');

// check number of elements (zero equals false, anything else equals true)
if (result.length) {
    result.click(function(event) {
        // blah blah
    });
}

result.length is same as result.size().

Binding event handlers to non existing elements

If your interface will generate new elements and you'd like them to have eevent handlers attached when they get added to DOM, then you should use delegate() function. There's also live() function but use the first one whenever possible, because it is a better alternative. There are numerous resources on the net why this is true. This Stackoverflow answer for instance.

Note: As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation.

See jQuery documentation.

like image 79
Robert Koritnik Avatar answered Oct 31 '22 22:10

Robert Koritnik


$("selector") will always return a jquery object that has the list of elements that have matched your selector.

if($("#nonexistantelement")) { /* this always happens*/ }

Whatever is inside your if will always be true, because Objects are truthy. If you really wanted do that then you'd want to say something like:

if($("#nonexistantelement").length > 0) { /* this only happens if you have something */ }

That will tell you if you have actually matched to your elements. But there is no harm in just calling .click(function) on an empty jquery set. Because all jquery does is iterate over the elements matched by the selector and apply that listener. If there are no elements in the set, then it doesn't get applied, and as essentially a noop.

Now if you want to bind callbacks to elements that dont exist yet, then look into .live() and delegate()

like image 3
J. Holmes Avatar answered Oct 31 '22 23:10

J. Holmes