4

I have a rather bizarre situation where I am trying to iterate through check boxes using each. Problem is it does not want to go in the loop.

Please advise as to why?

This is the function

function AddTheProduct() {
    var txtTopicsGuids = "";

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

and the markup

{{each Items}}
    <tr>
        <td>
            <input type='hidden' name='hidTopicsDomain' value='${DomainObjectKey}'/>
            <input type='checkbox' name='chkRelatedTopics' value='${subject}'/>
        </td>
    </tr>
{{/each}}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Arianule
  • 8,811
  • 45
  • 116
  • 174
  • 1
    I think you should use `$.each` here. – Mr_Green Sep 16 '13 at 13:45
  • 1
    They do the same thing. – simonzack Sep 16 '13 at 13:45
  • 1
    What does `console.log(checkedTopics.length);` give you? – Jason P Sep 16 '13 at 13:46
  • 1
    Why do you think its doesn't access the loop? – Tomzan Sep 16 '13 at 13:46
  • @Ram: No, `checkedTopics` should be a `NodeList`, because `getElementsByName` returns a `NodeList`. – Felix Kling Sep 16 '13 at 13:47
  • Your code works fine for me in jsfiddle: http://jsfiddle.net/ did you consider the case where the checkboxes are not checked? – simonzack Sep 16 '13 at 13:49
  • 2
    **Aside:** if you have no `else` condition then consider being more specific in your selector. `$('input[name=chkRelatedTopics]:checked').each( ...` – gvee Sep 16 '13 at 13:52
  • Nice docs for you (just in case): http://www.jquerysdk.com/api/template-tag-each – Andrei Sep 16 '13 at 13:52
  • @FelixKling, `document.getElementsByName()` returns an `HTMLCollection` not a `NodeList`. – Anthony Sep 16 '13 at 14:19
  • @Arianule - Was this issue reproducible in multiple browsers? There are bugs specific to the returned object type for `getElementsByName` and certain browsers. Since Matias was able to use the sample code in a fiddle, perhaps it's not elsewhere in your code but in your test environment. – Anthony Sep 16 '13 at 14:21
  • @Anthony: Actually that differs from browser to browser. The `HTMLCollection` interface is a super set of the `NodeList` interface, so it doesn't really matter (FWIW, according to the DOM2 specification the method should return a `NodeList`, see also http://stackoverflow.com/questions/15763358/difference-between-htmlcollection-nodelists-and-arrays-of-objects/15763707#15763707). – Felix Kling Sep 16 '13 at 14:56
  • @FelixKling - Pistols at dawn it is. :p – Anthony Sep 16 '13 at 15:01

3 Answers3

2

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 I recreated in jsFiddle.

Either the problem is in your template or somewhere else. Also, take into account the advice given in the other answers in terms of best practices, primarily make use of the appropriate selectors instead of doing getElementsByName and then wrapping that in a jQuery object.

mati
  • 5,218
  • 3
  • 32
  • 49
1

Use this:

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

   });
}
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
1

Try:

$('input[name="chkRelatedTopics"]').each(function(){
if($(this).is(":checked")){
//do something here
}
});
Kiran
  • 20,167
  • 11
  • 67
  • 99