Thursday, September 16, 2010

Bit field events

Are you struggling to get the onchange event to fire on a checkbox or radio button? Are you clearing your cache and refreshing your page and seeing no results? Well just the other day one of the other members on my team was having this issue and he came to me for some help. He had something like this
crmForm.all.new_bit.onchange = function() {
  alert(“I changed”);
};

and could not understand why he was not getting the alert when he clicked on the radio buttons for the bit field.

It only took me a second to see what was happening. First the onchange event will not fire until the radio button group looses focus or the blur() event is called on the radio button. After explaining this to my teammate I got the next logical question of, “Well how do you make the event fire when the user clicks on the radio button?” This was an easy one to answer, I said, “Replace onchange with onclick.” So he changed his to code to look like this
crmForm.all.new_bit.onclick = function() {
  alert(“I changed”);
};

So to recap for a checkbox or radio button
  1. onchange does not fire until radio button looses focus or blur() is called
  2. Use onclick if you want an event to fire as soon as a radio button is clicked.

No comments:

Post a Comment