Stephen Forlance
asked on
Jquery Ajax - on change event not being picked up
Hello All,
http://www.dnx.org/mep/form2.php
I have a simple form, when the select box changes if uses an AJAX method and jquery to retrieve another question and display it.
You can see it by selecting "Bird" as the pet, and then "This year" for when you bought it.
However, if the select box has been created by the ajax query it then does not trigger further ajax calls when its changed. (In the example given, it should display a further question when "This year" is selected.
Any ideas how I can get it working?
Thanks
http://www.dnx.org/mep/form2.php
I have a simple form, when the select box changes if uses an AJAX method and jquery to retrieve another question and display it.
You can see it by selecting "Bird" as the pet, and then "This year" for when you bought it.
However, if the select box has been created by the ajax query it then does not trigger further ajax calls when its changed. (In the example given, it should display a further question when "This year" is selected.
Any ideas how I can get it working?
Thanks
try
$('#form select').change(function() {
>>>
$('#form select').on ('change', function() {
$('#form select').change(function()
>>>
$('#form select').on ('change', function() {
ASKER
Hi all,
changing to $('#form select').on ('change', function() {
did not work
changing to $('#form select').on ('change', function() {
did not work
ASKER
Is there a way I could wrap this in a JS function and then call it via an onchange in the select tag?
Stephen,
When I browse to the page you referenced in your post, I can see from the console that it is binding the event to your new choices, and firing when I make changes. What is not working?
When I browse to the page you referenced in your post, I can see from the console that it is binding the event to your new choices, and firing when I make changes. What is not working?
I don't know if I clarified enough what I was proposing in my above comment. I was proposing switching to
The key part about this is that it has two selectors. The outer one "#form" is the selector to a wrapper element that is always on the page. The "select" is the selector to the element you are trying to have the change event listened for. By doing it this way, any new "Select" element that is added within the form will process the change event.
Hope this helps.
$( "#form" ).on( "change", "select", function() {
The key part about this is that it has two selectors. The outer one "#form" is the selector to a wrapper element that is always on the page. The "select" is the selector to the element you are trying to have the change event listened for. By doing it this way, any new "Select" element that is added within the form will process the change event.
Hope this helps.
ASKER
Thanks guys, its now working.
One final question, how would I set it up to bind both select and radio fields?
$( "#form" ).on( "change", "select", function() {
One final question, how would I set it up to bind both select and radio fields?
$( "#form" ).on( "change", "select", function() {
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Something like
Open in new window
instead of what you have with
Open in new window
For more info look here: http://api.jquery.com/on/