Billy
Billy

Reputation: 31

(php/jquery) hide and show checkboxes according to another checked checkbox value

i have a table in my database like this:

------------------------------
| ID_1         | ID_2        |
------------------------------
| A            | 1           |
| A            | 2           |
| A            | 3           |
| B            | 1           |
| B            | 2           |
| B            | 5           |
| C            | 1           |
| C            | 4           |
| C            | 3           |
------------------------------

and i have some checkboxes for ID_1 like this

<input class="ID_1" type="checkbox" value="A">
<input class="ID_1" type="checkbox" value="B">
<input class="ID_1" type="checkbox" value="C">

i'm trying to do that each time i checked the ID_1 checkbox, the ID_2 checkbox will show, depends on ID_1 checkbox i checked according to the table above. so if i checked two checkboxes

<input class="ID_1" type="checkbox" value="A" checked>
<input class="ID_1" type="checkbox" value="B" checked>
<input class="ID_1" type="checkbox" value="C">

it'll show like this:

<input class="ID_2" type="checkbox" value="1">
<input class="ID_2" type="checkbox" value="2">
<input class="ID_2" type="checkbox" value="3">
<input class="ID_2" type="checkbox" value="5">

is that possible using jquery? i need your help please

Upvotes: 2

Views: 399

Answers (1)

shivgre
shivgre

Reputation: 1173

This is possible using jquery, you need to display all the checkboxes and use class instead of Id. I am working on a solution will post once done.

Here is working solution for you. You can just add this in a script and run in browser.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!DOCTYPE html >
<html>
    <head>
        <script src="javas/jquery-1.12.2.min.js" type="text/javascript"></script>  
        
        <script type="text/javascript">
            $('document').ready(function () {
                //OBJECT OF ARRAY WITH YOUR DESIRED VALUES FROM DATABASE, THIS COULD BE MADE DYNAMICALLY
                var id1Obj = { A:['1','2','3'], B:['1','2','5'], C:['1','3','4'] }; 

                $('.ID_1').click(function () {
                    
                    $('.ID_2').hide();
                    jQuery('.ID_1:checked').each(function( index ) {
                           
                        var valId1CheckboxVal = $(this).val();                        
                        
                        jQuery('.ID_2').each(function( index ) {  
                            
                            
                            if( $.inArray( $( this ).val(), id1Obj[valId1CheckboxVal] ) != -1 )
                            { 
                                $(this).show();
                            }
                            else
                            {
//                                console.info("flse for " + $( this ).val());
//                                console.log( $(this) );
                            }
                        });
                    });
                    
                });
                
                
                

            });
        </script>
    </head>
    <body>

        <div style="width:100%">
            <label>ID_1 checkboxes</label><br>
            <input class="ID_1" type="checkbox" value="A"> A<br>
            <input class="ID_1" type="checkbox" value="B"> B<br>
            <input class="ID_1" type="checkbox" value="C"> C<br>
            
            <br>
            
            
        </div>
        <br>
        <div style="width:100%">
            <label>ID_2 checkboxes</label><br>
            
            <input class="ID_2" type="checkbox" value="1">1 <br>
            <input class="ID_2" type="checkbox" value="2">2 <br>
            <input class="ID_2" type="checkbox" value="3">3 <br>
            <input class="ID_2" type="checkbox" value="4">4 <br>
            <input class="ID_2" type="checkbox" value="5">5 <br>
        </div>
        
    </body>
</html>

Upvotes: 1

Related Questions