views:

446

answers:

1

Hi all,

I'm using YUI 3 to let someone click "Select All" or "Select None" and then have the selectbox select all the items or unselect all of them, respectively. Here's my code:

// This selects all
    Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.removeAttribute('selected');
               this.setAttribute('selected','selected');
            });
        }, selectAllNode
    );

// This selects none
   Y.on('click',function (e) {
            selectBoxNode.get("options").each(function () {
               this.setAttribute('selected','false');
               this.removeAttribute('selected');
            });
            selectBoxNode.('selectedIndex',-1);
        }, selectNoneNode
    );

selectAllLink, selectNoneLink, and selectBoxNode are self-evident, properly returned Nodes. Update: selectAll works, I had to manually remove the 'selected' attribute for each and re-add it.

The selectNoneLink doesn't work: it unselects only the elements that weren't before selected... although DOM inspection shows that the selectedIndex attribute is indeed changed to -1, so maybe it needs a refresh?

Any help would be appreciated. If this happens in all frameworks, that would be nice to know as well.

Thanks!

+1  A: 

This worked for me.

YUI().use('node', function(Y) {
   Y.get('#unsel').on('click', function(e) {
     Y.get('select').get('options').set('selected',false);
   });
   Y.get('#sel').on('click', function(e) {
     Y.get('select').get('options').set('selected', true );
   });
});
seth
Thanks, it worked beautifully. I think the errors came from trying to this.removeAttribute('selected') on "options" that weren't selected, but they weren't being reported so stuff just stopped working after a couple clicks. Thanks again!
Jasie
Glad it worked.
seth