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
Stephen ForlanceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
Yeah, the issue is that when you function initially runs the jquery binds to all the elements on the page.  When you ajax in something new it doesn't look at it again.  You have two options.  Re include the JavaScript when you run your ajax, or a better one you can use the on function and use event-delegation.

Something like

$( "#form" ).on( "change", "select", function() {
  // your function here
});

Open in new window


instead of what you have with
$('#form select').change(function() {

Open in new window


For more info look here: http://api.jquery.com/on/
3
HainKurtSr. System AnalystCommented:
try

$('#form select').change(function() {

>>>

$('#form select').on ('change', function() {
0
Stephen ForlanceAuthor Commented:
Hi all,
changing to  $('#form select').on ('change', function() {

did not work
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Stephen ForlanceAuthor Commented:
Is there a way I could wrap this in a JS function and then call it via an onchange in the select tag?
0
Jim RiddlesPrepress/OMS SpecialistCommented:
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?
0
Jeffrey Dake Senior Director of TechnologyCommented:
I don't know if I clarified enough what I was proposing in my above comment.  I was proposing switching to

$( "#form" ).on( "change", "select", function() {

Open in new window


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.
1
Stephen ForlanceAuthor Commented:
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() {
0
Jeffrey Dake Senior Director of TechnologyCommented:
You can use a comma in-between selectors to target multiple targets.

$( "#form" ).on( "change", "select, input[type=radio]", function() { 

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.