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!!
2 Answers 2
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
1 Comment
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.
1 Comment
$('#get_feed').load('/?ajax'; return false;} ... The goal is for me to get "news" that are related to the region selected in the checkbox and load them in the <div id="get_feed_frm"> that come later in the html code.