1

I have created a couple of custom events, "check" and "uncheck". I can easily use jQuery to implement these like this:

$("input:checkbox,input:radio").on("click",function(){
  if (j$(this).is(":checked")) {
    j$(this).trigger({
      type: "check"
    });
  } else {
    j$(this).trigger({
      type: "uncheck"
    });
  }
});

How can I allow for inline binding of this event?

<input type="checkbox" oncheck="fee('arg1','arg2')" onuncheck="fi('arg1','arg2')" />

Edit

My start to implement this could be somewhere along the lines of this:

$("[oncheck]").each(function(){
  $(this).on("check",function(){
    eval($(this).attr("oncheck"));
  });
});
$("[onuncheck]").each(function(){
  $(this).on("uncheck",function(){
    eval($(this).attr("onuncheck"));
  });
});

However, although I know it would work, I'm very opposed to using eval for this.

Samuel Reid
  • 1,756
  • 12
  • 22
  • What problem are you trying to solve? HTML has a limited set of attributes for hooking events anyway: http://www.w3schools.com/tags/ref_eventattributes.asp – Tallmaris Jul 05 '13 at 16:09
  • Edited question. Basically I've created these custom events which work when I use `$(something).on("check")` and `$(something).on("uncheck")`, but I need to be able to tie my custom event to the corresponding inline attribute. – Samuel Reid Jul 05 '13 at 16:13
  • I'm struggling to understand the approach. I'm guessing that you need to store extra data in the checkbox (like its price or something). If so, you could use [data attributes](http://html5doctor.com/html5-custom-data-attributes/) and have your onclick event look there for instructions. – Moob Jul 05 '13 at 16:26
  • I guess I'm sticking with eval. Kinda makes sense I guess. – Samuel Reid Jul 05 '13 at 17:28

0 Answers0