Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) Programming AJAX AJAX Callbacks

Dustin Henry
Dustin Henry
7,392 Points

Why do we do xhr.onreadystatechange = function instead of xhr.onreadystatechange(function)?

For instance, if I was adding an Event Handler to a DOM level object, I would do object.addEventListener('click', function), but with the onreadystatechange event handler, an equals sign is used instead. Why is that? Thank you for the help!

1 Answer

Steven Parker
Steven Parker
231,172 Points

The "addEventListener" method establishes an event handler for an object, but "onreadystatechange" is an event handler.

It might make more sense if we compare two ways of setting up "myClickHandler" on some object:

myButton.addEventListener('click', myClickHandler);  // this is one way
myButton.onclick = myClickHandler;                   // this is another

Both of those do (almost) the same thing. The advantage of "addEventListener" is that you can attach more than one handler that way.