Reputation: 401
I am using jQuery and Django.
I have the html below
<input name = "get_feed" type="submit" value="Get Feed" />
<div id="category">
<input type="checkbox" name="Europe" value="Europe"> Europe
<input type="checkbox" name="Africa" value="Africa"> Africa
<input type="checkbox" name="MEA" value="MEA"> MEA
</div>
I am trying to write a jquery function that would creates an array called "category"; each entry in "category" will be associated to the value of the checkbox, if it is checked.
i.e. category can be empty if no checkbox was checked, first entry would be "Europe" if europe is checked ect..
The whole point is to be able to sumbit the array in a Get method.
Question:
I would really appreciate if I can receive some help/tips that would help me create that array.
thx in advance!!
Upvotes: 1
Views: 107
Reputation: 2256
That being said, For ajax-compatibility
See demo on http://jsfiddle.net/xsSuH/4/
$(function() {
var category = new Array();
$("#getFeed").click(function() {
$.each($("input[name='country[]']:checked"), function() {
category.push($(this).val());
//alert($(this).val());
});
alert(category);
category.length = 0; //clearing the array
});
});
and use label for checkbox
Upvotes: 1
Reputation: 15358
You don't need jquery for that.
<div id="category">
<input type="checkbox" name="category[]" value="Europe"> Europe
<input type="checkbox" name="category[]" value="Africa"> Africa
<input type="checkbox" name="category[]" value="MEA"> MEA
</div>
Now when you submit you will see that category array contains any checked items.
Upvotes: 0