in onblur i called two different events; but its not working. First event only working.
How to do?
either in onBlur() or onFocus()
Example:
onblur="a();b();"

Dani AI

Generated

reported two handlers attached to an onblur only ran the first; recommended moving away from inline handlers. Useful, practical checks and fixes that solve the real causes you will encounter in plain JavaScript.

Common quick checks:

  • A runtime error in the first handler will stop the rest. Open the browser console and watch for stack traces when the blur happens.
  • Assigning via the onblur property can overwrite earlier assignments. Search your code for multiple onblur assignments.
  • Passing a call instead of a reference executes the function immediately (e.g., el.onblur = fn(); vs el.onblur = fn;).
  • blur does not bubble. If you want to catch focus loss on a parent, use the bubbling alternative or attach listeners to each element.
  • Dynamic DOM replacement or frameworks can remove handlers; confirm the element you attach to is the live element.

Safe fixes and patterns:

  • Prefer addEventListener so multiple listeners are preserved.
var el = document.getElementById('myField');

el.addEventListener('blur', handlerOne);
el.addEventListener('blur', handlerTwo);
  • To ensure one handler error does not stop others, call them from a wrapper and isolate errors:
el.addEventListener('blur', function (e) {
  try { handlerOne(e); } catch (err) { console.error(err); }
  try { handlerTwo(e); } catch (err) { console.error(err); }
});
  • If your goal is parent-level handling, use the bubbling focusout event instead of blur.

For API specifics and behavior see MDN: addEventListener and focusout event.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

Attach an event listener instead of inline js.

losing focus supports bubbling (i.e. if will fire if a parent loses focus too). blur is stuck to the control specified.

Are you using a lib such as jQuery or just using vanilla js?

No i am not using a lib such as jQuery or just using vanilla js.I am new to this one

Member Avatar for Member #120589

You're not supplying any code. Can't help you unless you show you're code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.