Monthly Archives: December 2014

jQuery: How to exit $.each loop

If you wrote your code and tried using the “break;” command to exit the “each loop” you may have noticed that it’s not working, thus is why you’re here.

The “break” command actually acts exactly what the “continue” command does in Javascript. The “continue” command skips the current iteration and continues to the next one.

To exit jQuery’s $.each loop, you have to use the “return false;” method, like so:

Happy coding 🙂

Javascript: and Event.srcElement

When working with events and making your code compatible with other browsers it’s important to remember to use the right target property depending what browser you are in.

For example if you are using Firefox then you can simple do this:

This will output an element which trigger the event. Now if you want to achieve the same thing in Internet Explorer (versions 6-8) you have to use different approach because event listeners are attached using a non-standard element.attachEvent() method.

To make things shorter and better, I recommend using a short notation to target both situations at the same time depending on the browser the client is using, like this:

You can read up more about this here.