4

How we can write code for onChange function in YUI 2 and YUI3.

jQuery(':input[type=radio]').change(function(){

  var aType=jQuery(this).val();
  var numT= aType==1 ? "3" : "6" ;
  var aWidth=aType==1 ? "330px" : "660px" ;

});
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Wasim Shaikh
  • 6,880
  • 18
  • 61
  • 88

1 Answers1

9

In YUI 3

Y.all('input[type=radio]').each(function (node) {
    node.on('change', function () {
        var val = this.get('value');
        ...
    });
});

// or
Y.all('input[type=radio]').on('change', function (e) {
    var val = e.currentTarget.get('value');
    ...
});

In YUI 2.9 (which is no longer under active development; use YUI 3)

// typical implementations alias Event or Event.on and
// Selector.query to shorter names
YAHOO.util.Event.on(
    YAHOO.util.Selector.query('input[type=radio]'), 'change', function (e) {
        var val = this.value;
    ...
});
Luke
  • 2,571
  • 15
  • 10
  • Thanks! but when I tried this in JSFiddle its alert both value 1,2 here is the URL http://jsfiddle.net/wasim117/cEYBv/ – Wasim Shaikh Aug 16 '11 at 05:12
  • Sorry! I broke my rule of never using nodelist.on() --delegation is better. The reason you got both values is that 'this' in nodelist subscriptions is the nodelist, not the individual node that received the event. e.currentTarget is the node, though. I've updated the code snippet. – Luke Aug 18 '11 at 03:35
  • thank you man, I'm using 2.9 yet, and your solution works well! BTW. you can also use .addListener(..) instead of .on(..) – Kostanos Jun 05 '13 at 03:11
  • @Luke for YUI 2.9 I think `Event.on` does not catch elements that got added dinamycaly to the form later on, and `delegate` has a problem with `change` for IE7, any idea about other solution ? I tried `blur` with delegate but it seems it is not supported. – Mehdi Karamosly Sep 17 '13 at 21:59