tags:

views:

362

answers:

2

Is it possible to apply / create a YUI Button by using an element's class name and not by id. I have to generate a list of buttons then transform it to a YUI button.

[Update]

By the way, I'm trying to apply the button in an anchor tag. So it will be a link button.

[Update:Code]

Ok so here's the code. I have a loop that generates this anchor tag.

<a class="system-button" href="/system/edit/12">Edit</a>

wrumsby's answer perfectly makes sense. But I don't know why it doesn't work. I tried debugging it and the elements are successfully fetched. But it seems that no YUI Buttons are created.

I even tried generating unique ids for the elements but still no luck.

Any ideas?

A: 

You should be able to use the srcelement configuration attribute like so:

(function() {
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event,
        Button = YAHOO.widget.Button;

    Event.onDOMReady(
        function() {
            var elements = Dom.getElementsByClassName('...');

            for (var i = 0; i < elements.length; i++) {
               var button = new Button({
                   srcelement: elements[i],
                   ...
               });
               ...
            }
        }
    );
})();
wrumsby
Hi. Thanks for the quick reply but I can't make it work. It doesn't convert them to YUI Buttons.
Gian Basagre
Are you able to post some code so we can get a better idea of the situation you're trying to deal with?
wrumsby
A: 

Looks like I've solved it myself. But I'm not sure if this is the best solution. I generated unique ids then create the buttons.

    var i = 0;
$(".system-button").each(function(i,b){

 var button = new YAHOO.widget.Button($(b).attr('id','system-button'+i).attr('id'));
  i++;
});

And oh yes, I use JQuery here. That framework is so awesome.

Gian Basagre