groenroos / minimalect

Minimal select replacement for jQuery
350 stars 52 forks source link

Request: Send in the element on onchange #50

Closed peirix closed 10 years ago

peirix commented 10 years ago

I have several selects on my page, and I just loop through them to create minimalects. But when they are changed I need to know which one was changed.

//this would be really nice
opts.onchange = function(val, text, elm) {}
groenroos commented 10 years ago

As far as I can recall, $(this) should give you a reference of the individual select in question inside the onchange callback. Does that not work in your case?

-----Original Message----- From: "peirix" notifications@github.com Sent: ‎15.‎8.‎2014 12:02 To: "groenroos/minimalect" minimalect@noreply.github.com Subject: [minimalect] Request: Send in the element on onchange (#50)

I have several selects on my page, and I just loop through them to create minimalects. But when they are changed I need to know which one was changed. //this would be really nice opts.onchange = function(val, text, elm) {} — Reply to this email directly or view it on GitHub.

peirix commented 10 years ago

No, it gives me a reference to the minimalect object, but not the DOM-element.

patrickgalbraith commented 10 years ago

@peirix I think this should be fixed by my recent commit see #48 keep in mind you will need to use the un-minified version.

$("select").minimalect({
   onchange: function(value, text) {
       this.element //...
       $(this.element) //...
   }
});
peirix commented 10 years ago

Ah. I ended up doing a workaround, which works just as fine, where I take advantage of closures

function initDropdown($elm) {
  $elm.minimalect({
    onchange: function(value, text) {
      $elm //points to my original select, which is what I need
    }
  });
}

$('select').each(function() {
  initDropdown($(this));
});