views:

19

answers:

2

I have a method defined this way:

var product = $('#selProduct:visible');
var note=$('#bbdata');

product.selectProduct({
    target: note,
    url: 'product.php',
    data: { ajax: true }
}).trigger('change');

$.fn.selectProduct= function (options) {

        var defaults = {
        key: "id",
        value: "label"
    };
    var settings = $.extend({}, defaults, options);

    if (!(settings.target instanceof $)) settings.target = $(settings.target);

    return this.each(function () {
        var $$ = $(this);
        $$.bind('change isVisible', function () {
            var data = null;
            if (typeof settings.data == 'string') {
                data = settings.data + '&' + this.name + '=' + $$.val();
            } else if (typeof settings.data == 'object') {
                data = settings.data;
                data[this.name] = $$.val();
            }

            $.ajax({
                url: settings.url,
                data: data,
                type: (settings.type || 'get'),
                dataType: 'html',
                success: function (j) {
                    settings.target.html(j);
                }
            }); // ajax
        }); //change isVisible
    });  // new function
};  // function

function show_product() {
    hide_group2();
    $("#product_zone2").show('fast', function(){
    $("#selProduct").trigger('isVisible');
    });
}

As you see the selector has the :visible filter. This code works ok, when the initial element is visible. But doesn't work on the items that are loaded with display:none. I made the div visible by using show() but the events are not picked up. The events are defined in the selectProduct method.

How can I make sure that this works on the items that were made visible after the page have loaded?

A: 

Where the event is defined ? If the element is not visible at start you have to use the live() function to affect it.

MatTheCat
Events are added in the selectProduct method defined somewhere else. I've updted the question to include that too.
Pentium10
So use the live() function to attach event soon the targeted element appear.
MatTheCat
Check the source code I attached, the function is defined somewhere else in some other file. I don't want to attach the event all the time when an element is appears. I want to have this working in some global way. The method is used for many items of the same type on different pages.
Pentium10
A: 

perform the selection var product = $('#selProduct:visible'); in an event which occurs after the page has loaded and the elements have been made visible.

NimChimpsky
Elements are changing visibility multiple times after the page has loaded. So I need something that adapts.
Pentium10
and what event alters the visibility ... ?
NimChimpsky
A change event on a radio button. I've updated the code to see how the events are defined.
Pentium10
can you select visible items in that change function then ?
NimChimpsky
That's what I want to avoid.
Pentium10
The selecter only picks up those elements visible when its initialized; I guess you could add a hidden element to the dom, the jQuery live function applied to that hidden element, and the selector would therefore be initialized when the element is added, ie when the other element made visible. Seems a bit over the top though.
NimChimpsky