User talk:Alex brollo/Bottoni.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Hi Alex, I've added a sample on how I would design this function.

If you name a global function "newButton", it shouldn't be specific about something: It should not append the elements itself to a fixed node (If you want this, you can name the function like createPanelButton.

If you name the parameter one nome, it shouldn't be an image. That's confusing. Instead, use multiple parameters.

You don't have to .append($(html)). Either do

$elem.append(html);

or better (because it is explicit and will work in future)

$elem.append($.parseHTML(html));

I've also shown how you can avoid setting the onclick attribute/ working with strings. However, note that when you directly pass a reference to a function to the jQuery event handler (click: callback), the function must be defined.

If you e.g. do the following

var _aFunction;
$createButton("label", "", "title", aFunction);
_aFunction = function(e) {
   // Do useful stuff here
};

the "useful stuff" is never executed. Why? Because you passed undefined as last parameter to $createButton. At the time, jQuery tried to add your function to the event-observer-queue, it just got an undefined.

Best -- Rillke(q?) 17:09, 10 October 2012 (UTC)[reply]