'Limit to checkbox selection with radio button but keeping previously checked values

I have 4 sets of radio buttons and based on the radio button selection, want to limit the checkbox selection while keeping previously selected values.

For example, for option 1(radio button), only 4 checkboxes can be selected, and for option 2( radio button), the limit is 6 checkboxes but if I select option 2 after selecting 4 values(checkboxes) from option 1 then the second option should have 2 more values to select keeping previously selected values.

Any help would be appreciated.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="radio_cont">
<input type="radio" name="box" value="box_4"/>pack of 4
<input type="radio" name="box" value="box_6"/>pack of 6
<input type="radio" name="box" value="box_9"/>pack of 9
<input type="radio" name="box" value="box_11"/>pack of 11
</div>

<div class="result_sel">
<input type="checkbox" name="gift" value ="1"/>One<br />
<input type="checkbox" name="gift" value ="2"/>Two <br />
<input type="checkbox" name="gift" value ="3"/>three<br />
<input type="checkbox" name="gift" value ="4"/>four<br />
<input type="checkbox" name="gift" value ="5"/>five<br />
<input type="checkbox" name="gift" value ="6"/>six<br />
<input type="checkbox" name="gift" value ="7"/>seven<br />
<input type="checkbox" name="gift" value ="8"/>eight<br />
<input type="checkbox" name="gift" value ="9"/>nine<br />
<input type="checkbox" name="gift" value ="10"/>ten<br />
<input type="checkbox" name="gift" value ="11"/>eleven<br />
</div>


Solution 1:[1]

you can put the free_play_or_preview and id parameters in "changSRC" and give parameters according to the song. In my solution if free_play_or_preview parameter is "f" image is free play image else image is preview image and you can find which element has which image with id parameter

Here is my solution :

<!DOCTYPE html>
<html>
<body>

<audio id="audio1">
   <source src="/songs/Hall-of-the-Mountain-King.mp3" type="audio/mpeg">
   <source src="/songs/Hall-of-the-Mountain-King.ogg" type="audio/ogg">
</audio>

<a href="javascript:play('1');"><img id="img1" src="/images/Free_Play.png" align="center" width="100%" alt="Free Play" onclick="changSRC('f',1)" /></a>

<audio id="audio2">
   <source src="/songs/Rimsky-Korsakov-The-Flight-of-the-Bumble-Bee.mp3#t=0,15" type="audio/mpeg">
   <source src="/songs/Rimsky-Korsakov-The-Flight-of-the-Bumble-Bee.ogg#t=0,15" type="audio/ogg">
</audio>

<a href="javascript:play('2');"><img id="img2" src="/images/Preview.png" align="center" width="100%" alt="Song Preview" onclick="changSRC('p',2)" /></a>

<script>
    function play(id){
       var audio = document.getElementById('audio'+id);
       audio.play();
    }
    
    function pause(id){
       var audio = document.getElementById('audio'+id);
       audio.pause();
    }
    
    function changSRC(free_play_or_preview,id) {
        if(document.getElementById(`img${id}`).src == "/images/Song_Pause.png"){
            if(free_play_or_preview == "f"){
                document.getElementById(`img${id}`).src == "/images/Free_Play.png"
            } else {
                document.getElementById(`img${id}`).src == "/images/Preview.png"
            }
        } else {
            document.getElementById(`img${id}`).src="/images/Song_Pause.png";
        }
    }
</script>

</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hasan Ekmen