Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my jquery .on('change') not working for dynamically added selects

Tags:

I'm adding select elements dynamically, like in the below HTML. I'm not sure why the .on('change' ...) is not working for the dynamic select. What am I missing?

I'm using Chrome 24.0.1312.57 + jquery 1.8.3.

<script type="text/javascript">   $(document).ready(function() {       $('#x select').on('change', function () { alert('helo'); })       $('#y select').on('change', function () { alert('helo'); })        $('#x').html($('#y').html());   }); </script>  <div id="x"></div> <div id="y">     <select>         <option>O1</option>         <option>O2</option>     </select> </div> 
like image 235
jaime Avatar asked Feb 18 '13 17:02

jaime


People also ask

Does jQuery work on dynamic content?

You have to add the selector parameter, otherwise the event is directly bound instead of delegated, which only works if the element already exists (so it doesn't work for dynamically loaded content). The jQuery set receives the event then delegates it to elements matching the selector given as argument.

How dynamically add HTML element using jQuery?

$(document). ready(function() { $('body'). on('click', '. AddEl', function() { var $elems = $('.

What is the use of change in jQuery?

Definition and Usage The change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected. For text fields or text areas, the change event occurs when the field loses focus, after the content has been changed.

How can show dynamic data in jQuery?

In this example, we should go with the following steps to implement dynamic data loading using jQuery. Create UI to be appended for each user information. Download and import latest version of jQuery library files and jQuery UI files. Create custom jQuery handlers to append UI and load dynamic data into it.


2 Answers

Your code:

$('#x select').on('change', function () { alert('helo'); }) 

attaches an event handler to the select inside the #x element.

What you want (from what i understood) is something in the lines of:

$("#y").on('change','select',function () { alert('helo'); }); 

This attaches an event handler to the #y element that gets delegated to its children 'select' elements

From http://api.jquery.com/on/

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object.

like image 188
cernunnos Avatar answered Oct 24 '22 00:10

cernunnos


Event binding to elements that are not in the DOM on initial page load will not work. You need to bind to an element further up the DOM that exists to allow the event to trickle down. This is usually the approach that I take:

$(document).on({   change: function() {     alert('helo');   } }, '#x select');  $(document).on({   change: function() {     alert('helo');   } }, '#y select'); 

I prefer it as you can add subsequent events easily.

$(document).on({   change: function() {     alert('helo');   },   blur: function() {     alert('helo');   } }, '#x select'); 
like image 34
d_ethier Avatar answered Oct 23 '22 23:10

d_ethier