1

I am new to jQuery and I want to loop through the elements of a form, however there are textareas that have class with 'hide' and I want to continue to the next element. I have the following code:

jQuery(document).ready(function(){jQuery('#helpdesk_ticket_submit').on('click', function(e){
      submitText = ""
      jQuery('#new_helpdesk_ticket input, #new_helpdesk_ticket select, #new_helpdesk_ticket textarea').each(
        function(index){  
            var input = jQuery(this);
            if (input.prev().is('textarea'))
              if (input.attr('class').indexOf('hide') >= 0) 
                continue;

            submitText += "<p><b>" + input.attr('id') + "</b>" + "<code>: </code>" + input.val() + "</p>"
        }
      );
      window.open().document.write(submitText);
  });
});

I am getting the following error: Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement

How is it possible to continue the loop when it finds a textarea with a class with the word hide?

Thanks

Sascha
  • 4,576
  • 3
  • 13
  • 34
  • 1
    Does this answer your question? [How to use continue in jQuery each() loop?](https://stackoverflow.com/questions/17162334/how-to-use-continue-in-jquery-each-loop) – Justinas Sep 08 '20 at 12:27
  • Check this ["How to use continue in jQuery each() loop?"](https://stackoverflow.com/questions/17162334/how-to-use-continue-in-jquery-each-loop#:~:text=We%20can%20break%20both%20a,immediately%20to%20the%20next%20iteration.) – JAS Sep 08 '20 at 12:32

1 Answers1

3

Use return; to continue the each-loop from jQuery.
If you want to break use return false;.

jQuery(document).ready(function(){jQuery('#helpdesk_ticket_submit').on('click', function(e){
      submitText = ""
      jQuery('#new_helpdesk_ticket input, #new_helpdesk_ticket select, #new_helpdesk_ticket textarea').each(
        function(index){  
            var input = jQuery(this);
            if (input.prev().is('textarea'))
              if (input.attr('class').indexOf('hide') >= 0) 
                return;

            submitText += "<p><b>" + input.attr('id') + "</b>" + "<code>: </code>" + input.val() + "</p>"
        }
      );
      window.open().document.write(submitText);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Sascha
  • 4,576
  • 3
  • 13
  • 34