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.

4
  • What problem are you trying to solve? HTML has a limited set of attributes for hooking events anyway: w3schools.com/tags/ref_eventattributes.asp Commented Jul 5, 2013 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. Commented Jul 5, 2013 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 and have your onclick event look there for instructions. Commented Jul 5, 2013 at 16:26
  • I guess I'm sticking with eval. Kinda makes sense I guess. Commented Jul 5, 2013 at 17:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.