Loop on jQuery object and return value


/ Published in: JavaScript
Save to your folder(s)

Returning a boolean inside of jQuery's $.each() function will only break the loop. To get a return value, you can convert the jQuery object to array and use a regular for loop in javascript.


Copy this code and paste it in your HTML
  1. var checkBoxes = $(':checkbox');
  2.  
  3. var allAreSelected = (function() {
  4.  
  5. for (i in checkBoxes.get()) {
  6. if (!checkBoxes[i].checked) {
  7. return false;
  8. }
  9. }
  10.  
  11. return true;
  12.  
  13. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.