0

I have a bunch of check boxes which a user can check one or more at a time. I need to grab all the checked check boxes into an array and then grab each values from the array and push each value to a query string variable.

<div class="Cat" style="width: 155px; float: left; margin-left:15px;">
    <p>Location</p>
    <div><input class="LocChk" type="checkbox" value="United States"/>&nbsp;United States<br/></div>
    <div><input class="LocChk" type="checkbox" value="Australia"/>&nbsp;Australia<br/></div>
    <div><input class="LocChk" type="checkbox" value="Canada"/>&nbsp;Canada<br/></div>
    <div><input class="LocChk" type="checkbox" value="China"/>&nbsp; China<br/></div>
</div>

So instead of hard coding the selected values in the following query variable, i want to pass the checked values from the array. Let's say a user checked Canada and China, I need to store those two values in an array and then in the following query variable, access them in order. Can someone please show me an efficient way to do this in jQuery?

var queryText = "<View>"+
                    "<Query>"+
                        "<Where>"+
                            "<In>"+
                                "<FieldRef Name='HSCountry' />"+
                                    "<Values>"+
                                        "<Value Type='Choice'>Canada</Value>"+
                                        "<Value Type='Choice'>China</Value>"+
                                    "</Values>"+                                                
                                "</In>"+
                        "</Where>"+
                    "</Query>"+
                    "<ViewFields><FieldRef Name='Title' /></ViewFields>"+
                "</View>";                          
0

2 Answers 2

4

Oh this is why I love the .map() function:

var checkboxArray = $(".LocChk:checked").map(function() {
    return this.value;
}).get();
Sign up to request clarification or add additional context in comments.

3 Comments

Oh man. That is sweet! I had never heard of the .map() function before!
@tymeJV How do I retrieve the values from the array so I can use them in the queryText variable?
You can iterate through it, so checkboxArray[0] is the first value. Or you could iterate over with a for loop and construct your string.
0
var values = [];
$.each('input[type=checkbox]:checked',function(){
    values.push($(this).val());
}); 

First, values is an array that you set up to hold the values of all the check boxes. Then, you use the jQuery $.each function to cycle through each input box that is checked. For each one that is checked, the value is added to the values array. Then you can cycle through them later on for whatever purpose you have.

1 Comment

Maybe just a tad more elaboration on what's happening here.

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.