0

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

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Wow first time I come across the jsfiddle website, I think that this is what I need :) thx!!
0

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

I think I understand the changes you made. However I cannot see how I can issue an ajax GET method by telling it to take "category" as variable to submit. I am using this code to request a GET $('#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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.