Reputation: 439
Hello i'm trying to check if the checkboxes are checked or not. However my checkboxes are created using a for loop and the value of each checkbox is actually the same.
Code is as follows:
{% for keyword in keyword_list %}
{% if keyword.keyword_name == userprofile.keywords_subscribed %}
<input type="checkbox" disabled="disabled" name="keywords" value="keywords"/> {{keyword.keyword_name}}<br />
{% else %}
<input type="checkbox" name="cb" value="keywords" /> {{keyword.keyword_name}}<br />
{% endif %}
{% endfor %}
Jquery code as follows:
<h2>Price of subscriptions</h2>
<script type="text/javascript" src="/static/js/jquery-1.6.3.min.js"></script>
<script type="text/javascript" src="/static/js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/static/fc3/charts/FusionCharts.js"></script>
<script type="text/javascript">
function popup(){
{% for price in price_list %}
alert({{price.price_of_each_keyword}})
{% endfor %}
}
function DoTheCheck() { if(document.cb2.checked == true)
{ alert('box2 is checked'); }
}
</script>
At the moment this is all i can think of.
So how do i use jquery to iterate through the checkboxes and check to see if they are checked?
I found some examples on the web but most of them actually created individual checkbox names for their code and this makes it easier to use jquery to check them.
Appreciate any help i can get.
Thank you.
Upvotes: 3
Views: 799
Reputation: 6771
You can get a list of all checked checkboxes in the site with this:
$('input[type=checkbox]:checked')
SHORT
As mentioned from Craig, there is a short version available as well:
$(':checkbox:checked')
COUNT
And if you want to know the no of checked checkboxes, simply do this:
$(':checkbox:checked').length
Upvotes: 6
Reputation: 16522
If you don't wanna loop thru all checkbox on the page
You could give them a class and loop like this
$('.yourClass').each(function(i, obj) {
if($(this).is(':checked'))
{
your code
}
});
Upvotes: 0
Reputation: 1926
Marc has a good answer, but if you want to go through each one and check then the following will do that.
$('input[type=checkbox]').each(function(){
if($(this).is(":checked")){
alert($(this).attr("name")+ " is checked");
} else {
alert($(this).attr("name")+ " is not checked");
}
});
hope that helps!
Upvotes: 0