Syndicate

Feed

Binding an event listener to a DOM element that may not yet exist

There's a very sweet jQuery plugin that makes it possible to bind event listeners to elements that may not have been added, yet, to the document. It's called livequery and I downloaded the 1.1.x version on github to work with my jQuery 1.3.2. (There's no 1.1.x version among the official releases on the jQuery site.) I used this plugin recently. The context was: another jQuery script was responsible for adding drop-down boxes to my web page. I wanted to do something on my end whenever a new selection was made in these drop-downs.

Just so that I remember... as a note to my future self... There's another way to bind an event to present and future elements, and that way relies on event bubbling:

jQuery(function() {
  jQuery('#myWrapperDiv').click(function(evt) {
    var targetedDOMElement = evt.target;
    if (jQuery(targetedDOMElement).is('#select-0 option')) {
      doMyThing.call(targetedDOMElement);
    }
  });
});
 
function doMyThing() {
  console.log(jQuery(this).parent().val());
}

I can bind an event to a container div element that exists already and which will contain my drop-down boxes. As a refresher, Function.call() receives as first argument whatever we want the keyword this, in the function called, to refer to.

The way of the livequery plugin is less verbose somewhat. (Note: some functionality in livequery did make it to jQuery's 'core' but you can't listen to a change event with jQuery.live() yet.)

This code does not need to be run when the DOM is ready:

jQuery('#select-0').livequery('change', function() {
  console.log(jQuery(this).val());
});
Last edited by Caroline Schnapp about 7 years ago.

Comments

Exactly what I was looking for..

.. thanks for posting.

Stunning site design by the way. The header with the cup is the prettiest thing I've seen on the web in a while.

Thank you, Rob!

I am glad this bit of coding and text were useful to you.

website is so great

Your website is so great. More on this website may think I'm surprised. That's how you understand this subject is nice to know.

There are many scams when it

There are many scams when it comes to the male enhancement industry. While there are some male enhancement pills that can make a difference, the majority of these products are standing on very shaky grounds.

now this is my problem and

now this is my problem and it's not yet fixed. I am going to try this codes that you shared here hoping that mine will be fixed. Emergency Dentist Utah

I can bind an event to a

I can bind an event to a container div element that exists already and which will contain my drop-down boxes. As a refresher, Function.call() receives http://e-papierossy.com.pl/en/e-papierosy/94-e-papierosy-gambit-9812.html as first argument whatever we want the keyword this, in the function called, to refer to.

The way of the livequery http://e-papierossy.com.pl/en/e-papierosy/95-e-papierosy-gambit-5412.html plugin is less verbose somewhat. (Note: some http://e-papierossy.com.pl/en/e-papierosy/96-e-papierosy-gambit-5609.html functionality in livequery did make it to jQuery's 'core' but you can't listen to a change event with

Yes you have your point

Yes you have your point there and it is so great to read your blog. Many of a element should be secure so that it will not even made some problems. For me you have a amazing blog and it is related to me your statement, got an good at all.

I am sure it will exists

I am sure it will exists someday and I know that everything still will be fine. beef jerky for sale

Jquuerry plugin makes all

Jquuerry plugin makes all the difference to programmers and web designers. that is why we need to also have it a try and i am sure this will make it easier for us in the future to manipulate a certain website.
Thank you,
janice of montreal web design company

I have been looking for this

I have been looking for this code for the couple of an hour, I am so grateful that you shared this code, thanks how to recycle electronics

Thanks for the script here,

Thanks for the script here, it really works great. . learn more

Generally speaking, the

Generally speaking, the blogs that have more readers and higher click rate are usually better written pmp practice exam

"(There's no 1.1.x version

"(There's no 1.1.x version among the official releases on the jQuery site.)"

honestly i was actually waiting for it. I hope they released it already. what is website conversion

great

if you are a drupal user then i am sure that you would know for this one. I would also love to be a drupal user too ad that's what i am longing right now. excess electrical

I have already paste the

I have already paste the page but still it is not working.. agile project management training course

very nice!!

I appreciate the great information. I arrived on the information I was looking for. Thank you as always.

Re:

This is an interesting article, thanks. dissertation examples

On reply

It was so funny how people try to comment in those days.

Thanks very much for an

Thanks very much for an excellent explanation. Also I much prefer your confirmation sum to trying to sort out some of the weird characters that others pop up. http://www.braungresham.com/2013/01/fiscal-cliff-deal-benefits-land-cons...

Thanks for the post Twitter

There's no 1.1.x version

There's no 1.1.x version among the official releases on the jQuery site.

Thanks for the post Twitter

I appreciate the great

I appreciate the great information. I arrived on the information I was looking for. Thank you as always.

http://kessiya.com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.