Reputation: 9043
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}}
Upvotes: 4
Views: 4760
Reputation: 5348
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.
Upvotes: 2
Reputation: 20313
Try:
$('input[name="chkRelatedTopics"]').each(function(){
if($(this).is(":checked")){
//do something here
}
});
Upvotes: 1
Reputation: 15603
Use this:
function AddTheProduct() {
var txtTopicsGuids = "";
$("input[name=chkRelatedTopics]").each(function() {
if ($(this).is(":checked")) {
//action
}
});
}
Upvotes: 1