JQuery.each doesn't seem to work

I have a rather strange situation where I try to iterate through checkboxes using each . The problem is that he does not want to go in a loop.

Please advise why?

This is a function.

 function AddTheProduct() { var txtTopicsGuids = ""; var checkedTopics = document.getElementsByName("chkRelatedTopics"); $(checkedTopics).each(function() { if ($(this).is(":checked")) { //action } }); 

and markup

 {{each Items}} <tr> <td> <input type='hidden' name='hidTopicsDomain' value='${DomainObjectKey}'/> <input type='checkbox' name='chkRelatedTopics' value='${subject}'/> </td> </tr> {{/each}} 
+4
source share
3 answers

Your javascript code should work. I think the problem has nothing to do with the $.each method, but with something else.

Here is a simplified example of your code that I recreated in jsFiddle.

Either the problem is in your template, or somewhere else. Also, keep in mind the recommendations given in other answers from a best practice point of view, first use the appropriate selectors instead of getElementsByName , and then wrap this in a jQuery object.

+2
source

Use this:

 function AddTheProduct() { var txtTopicsGuids = ""; $("input[name=chkRelatedTopics]").each(function() { if ($(this).is(":checked")) { //action } }); } 
+1
source

Try:

 $('input[name="chkRelatedTopics"]').each(function(){ if($(this).is(":checked")){ //do something here } }); 
+1
source

Source: https://habr.com/ru/post/1502418/


All Articles