2

I saw lots of similar question where you can extract the values of Checkboxes based on the check uncheck and add them to URL, but if we have different categories of checkbox group, separate them with &. Example:

$(document).ready(function() {
        var swapRelation = "";
        $("input[type=checkbox]").click(function(e) {
            var seasoning = "",
                parentRelation = "",
                tempArray = [];
            
            $("input:checked").each(function() {
                tempArray.push($(this).attr("name").replace(/\s/g, ''));
                parentRelation = $(this).closest(".wrapper").find('.catName').text().trim();
                parentRelation = parentRelation.replace(/\s/g, '');

            });
            if (tempArray.length !== 0) {
                seasoning += `${parentRelation}=` + tempArray.toString();
                // if (swapRelation == parentRelation) {
                //     // seasoning+=`&${parentRelation}=`+tempArray.toString();
                //     seasoning += `${parentRelation}=` + tempArray.toString();
                // }else {
                    
                // }
                
                //tempArray = [];
                swapRelation = parentRelation;
            }
            console.log("example.com?" + seasoning);
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
    <div class="catName">Fruits</div>
    <div class="checkBoxWrap">
        <input class="input__field" type="checkbox" name="apple" id="input-5">
        <input class="input__field" type="checkbox" name="banana" id="input-6">
        <input class="input__field" type="checkbox" name="mango" id="input-7">
    </div>
</div>
<div class="wrapper">
    <div class="catName">Vaegs</div>
    <div class="checkBoxWrap">
        <input class="input__field" type="checkbox" name="Okra" id="input-8">
        <input class="input__field" type="checkbox" name="Patato" id="input-9">
        <input class="input__field" type="checkbox" name="Tamato" id="input-10">
    </div>
</div>
<div class="wrapper">
    <div class="catName">Rivers</div>
    <div class="checkBoxWrap">
        <input class="input__field" type="checkbox" name="Ganga" id="input-11">
        <input class="input__field" type="checkbox" name="yamuna" id="input-12">
        <input class="input__field" type="checkbox" name="thames" id="input-13">
    </div>
</div>

Expected Result on multiple Selections:

URL?Fruits=banana,mango&Vegs=okra,patato&Rivers=ganga,whateverSelected

3
  • You probably want an each function for each category, so it builds its own array. Commented Sep 27, 2021 at 14:12
  • 3
    Why not give the checkbox groups the same name: name="fruits[]", with different values: value="apple", value="banana", value="mango", and let the URL construction happen naturally, like ?fruits[]=apple&fruits[]=banana&fruits[]=mango. Is there a reason you need it as a CSV in the URL? Commented Sep 27, 2021 at 14:13
  • How about join() with , as the separator. Commented Sep 27, 2021 at 14:14

1 Answer 1

1

You can use URLSearchParams to build your query string.

var usp = new URLSearchParams();
document.querySelectorAll('.wrapper').forEach((wrapperDiv)=> {
    var category = wrapperDiv.querySelector('.catName').textContent;
    var checkedBoxes = wrapperDiv.querySelectorAll('input[type="checkbox"]:checked');
    var values = Array.from(checkedBoxes, cb=>cb.name).join('');
    usp.append(category,values);
});
        
Sign up to request clarification or add additional context in comments.

Comments

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.