views:

32

answers:

2

I have a method setup to fire onChange with the following code:

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

product.selectProduct({
    target: note,
    url: 'product.php',
    data: { ajax: true }
}).trigger('change'); // TO DO: to fire also on visibility change

Then I have a method where I make a div visible.

function show_product() {
    hide_group2();
    $("#product_zone2").show('fast', function(){
    $("selProduct").trigger('isVisibleProduct');// custom trigger event
    });
}

I want to change the method definition to fire also when I raise the custom event. How do I do that?

A: 

Hi,

Just use jQuery bind to link your desired function to any type of event (event custom).

myObject.bind('myEventName', function(){/*..*/});

or

myObject.bind('myEventName', myFunctionName);

then trigger with

myObject.trigger('myEventName');

If your case you could put

product.bind('isVisibleProduct', function(){
  this.selectProduct({
      target: note,
      url: 'product.php',
      data: { ajax: true }
  }).trigger('change'); // TO DO: to fire also on visibility change
});
Alin Purcaru
`this.selectProduct({...])` I'm pretty sure this will run some error. ;)
Reigel
why do you say that?
Alin Purcaru
+2  A: 

With this code

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

are not binding selectProduct() to be fired on a change event. You just execute selectProduct() and then fire change on product. Maybe selectProduct() binds something to the change event. But without knowing what it does, it is hard to tell.

If you want to execute selectProduct() on the change event and your custom event, you have to bind an event handler to both events, like so:

product.bind('change isVisibleProduct', function() {
    $(this).productSelect({/* ...parameters here ... */});
});

Please clarify your question if this is not what you mean.

Felix Kling
The 2nd part is good, however I am not so sure about the first assumption, as I don't have any other bind method that binds the selectProduct to the onchange event in the page. Probably the ajax method definition does, but not sure.
Pentium10
+1 what I have in mind.
Reigel
@Pentium10: The problem is that if `selectProduct()` itself binds some method, everytime you execute the function again, it will bind that method again (in addition!) which you don't want.
Felix Kling
the event is called 'change', not 'onchange', try not to confuse them
Alin Purcaru