What is the difference between onBlur and onChange attribute in HTML?

158,685

Solution 1

The onBlur event is fired when you have moved away from an object without necessarily having changed its value.

The onChange event is only called when you have changed the value of the field and it loses focus.

You might want to take a look at quirksmode's intro to events. This is a great place to get info on what's going on in your browser when you interact with it. His book is good too.

Solution 2

onblur fires when a field loses focus, while onchange fires when that field's value changes. These events will not always occur in the same order, however.

In Firefox, tabbing out of a changed field will fire onchange then onblur, and it will normally do the same in IE. However, if you press the enter key instead of tab, in Firefox it will fire onblur then onchange, while IE will usually fire in the original order. However, I've seen cases where IE will also fire blur first, so be careful. You can't assume that either the onblur or the onchange will happen before the other one.

Solution 3

An example to make things concrete. If you have a selection thus:

<select onchange="" onblur="">
  <option>....
</select>

the onblur() is called when you navigate away. The onchange() is called when you select a different option from the selection - i.e. you change what it's currently selected as.

Solution 4

In Firefox the onchange fires only when you tab or else click outside the input field. The same is true of Onblur. The difference is that onblur will fire whether you changed anything in the field or not. It is possible that ENTER will fire one or both of these, but you wouldn't know that if you disable the ENTER in your forms to prevent unexpected submits.

Solution 5

I think it's important to note here that onBlur() fires regardless.

This is a helpful thread but the only thing it doesn't clarify is that onBlur() will fire every single time.

onChange() will only fire when the value is changed.

Share:
158,685
pacman
Author by

pacman

Updated on May 13, 2021

Comments

  • pacman
    pacman about 3 years

    When is one called versus the other? Is there a situation were onChange would be called but onBlur would not be called?

  • Chris Middleton
    Chris Middleton almost 9 years
    Another example: in inputs of type number, clicking the up/down arrows will fire a change event (but not a blur event), whereas typing only causes a change event when the field loses focus.
  • stackdave
    stackdave over 6 years
    your link about quirksmode don't talk about blur, just basic events
  • Indiana Kernick
    Indiana Kernick over 3 years
    Just to be crystal clear, there are no situations where change is fired but blur is not, correct?
  • Brian Ortiz
    Brian Ortiz over 2 years
    @IndianaKernick The Enter key would trigger change but not blur. Also, selecting an option in a dropdown would only trigger change since the dropdown still has focus.
  • ghost_programmer
    ghost_programmer almost 2 years
    Notice that you can’t use onChange to validate an empty field. The reason is that nothing is present when a form is first loaded, but since the form data hasn’t changed either, onchange won’t trigger even if a user navigates through empty form fields. onblur event solves this problem by always being triggered any time the input selection, or focus, leaves a field.