views:

152

answers:

2

Hello all,

I have a form with an id of "wizard" - I only have select elements in this form. This form is in a lightbox using the JQuery plugin fancybox:

I want to know when any of these have been changed using JQuery. How can I do this? I currently have:

$('form#wizard select[name=servers], form#wizard select[name=cores]').change(function() {
var channels = parseInt($('form#wizard select[name=servers]').val(), 10) * parseInt($('form#wizard select[name=cores]').val(), 10);
$('#yellow').val(channels);
});

EDIT - I have the above wrapped in $(document).ready(function() {...}

However, it does not work, it does not even get run. I have put alerts in there and they never show up. The above only works when the above is a div that I have removed the display:none from, strange! So I am looking for a different implementation to get around this as I need that lightbox as it is.

I really need help on this.

Thanks all

A: 

can you send whole code, do you use iframe?

jmav
No I do not use an iFrame.
Abs
+3  A: 

The jQuery change function only binds those elements that are present when the domready event fires. If the lightbox plugin you are using is dynamically creating elements, you should be using jQuery's live function to "bind your handler to all current - and future matched elements".

Change this:

$('your selector').change(function() { /* code ... */ });

with this:

$('your selector').live('change', function() { /* code ... */ });
mtyaka
Awesome! This is working better, but it seems it only accounts for changes once? Is this supposed to happen?
Abs
Actually its working but the span element with id "yellow" doesn't get updated, it gets updated when I close the lightox and repoen it? How can I get it to take effect as soon as a change is made?
Abs
My guess is that the span with id "yellow" gets copied by the lightbox plugin which means that you end up with two (or more) span elements with the same id. Since ids should be unique, jQuery only updates your original (hidden) span. Try using class instead of id.
mtyaka
Do you mean give it a class yellow and refer to it via the class name?
Abs
Yes, that's what I meant.
mtyaka
Sorry, I am a noob - How do I refer to a class? The same way?
Abs
Ok - it makes a change: $('span#[class=yellow2]').html(channels); - that worked! :)
Abs
If you have a span with class "yellow" (<span class="yellow">...</span>), you can do $('span.yellow').val(channels);
mtyaka
Thanks, I used that. But I have another problem, but I have open another question for this! More than welcome to help again on it! :)
Abs