Link to home
Start Free TrialLog in
Avatar of Alex Lord
Alex Lord

asked on

Checkbox control - storing attr into array

So here is my problem. i have a JS function that checked for check event and takes the attr of these check boxes and store them for later use, this function controls outputs for check condition of checkboxes, what i am trying to add to the function is to take the id attr of the checkboxes and store them into an array to be passed on during a condition. my issue is im not sure how to do this as multiple attempt failed any hints.

my function as follows -

function actProp() {
      
     var isChecked = $(this).is(":checked");
     var propChk = "#" + $(this).attr('name');
     var chkCon = "#" + $(this).attr('class'); 
     var chkState = $(this).attr('id'); 
     var chkStateCollection = [];

   chkStateCollection.push(chkState);
     if(isChecked) {   
        console.log(chkStateCollection);
       $(propChk).fadeIn(1000);
       $(chkCon).fadeIn(1000);
  
       //addChkState();
       
      
      
       
       return false;
       } 
     else {  
         event.stopPropagation();
        $(propChk).fadeOut(1000); 
        $(chkCon).fadeOut(1000);       
       }
       
    }

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

I think you should loop over all checkboxes everytime to maintain an good list of checked checkboxes :
    <script>
        jQuery(function() {

            function actProp() {

                var isChecked = $(this).is(":checked");
                var propChk = "#" + $(this).attr('name');
                var chkCon = "#" + $(this).attr('class');
                var chkState = $(this).attr('id');

                var chkStateCollection = [];
                $(":checkbox").each(function() {
                   if( $(this).is(":checked") ) {
                       chkStateCollection.push(chkState);
                   }
                });
                console.log(chkStateCollection);

                if(isChecked) {
                    $(propChk).fadeIn(1000);
                    $(chkCon).fadeIn(1000);
                    //addChkState();
                    return false;
                }
                else {
                    event.stopPropagation();
                    $(propChk).fadeOut(1000);
                    $(chkCon).fadeOut(1000);
                }
            }

           $(":checkbox").change(actProp);
        });
    </script>

Open in new window

Couple of things you'll need to look at.

Firstly, you'll need to pass in the checkbox to your function. this won't work. You haven't shown how you're calling the actProp() function, but something like this:

<input type="checkbox" onclick="actProp(this)">

And then declare your function like so:

function actProp(checkbox) {
    var isChecked = $(checkbox).is(":checked");
    ...

Open in new window

For the array, you're declaring it inside your function, so it's only available inside that function, and it will be recreated every time it's called. Declare it outside of the function.

You seem to be concatenating the name to a # symbol to get an ID. Can't see why you'd do that when all you need is the ID. If you're doing it to refer to other elements, then I would suggest you use data atributes instead.

var chkStateCollection = [];

function actProp(checkbox) {
    var isChecked = $(checkbox).is(":checked");
    var chkState = $(checkbox).attr('id');

    var chkCon = $(checkbox).data('someDataProp'); 
    var propChk = "#" + $(checkbox).data('anotherDataProp');

    chkStateCollection.push(chkState);
    ...
    if (isChecked) {   
        ...
    } else {  
        ...       
    }
}

Open in new window


<input type="checkbox" id="someId" data-someDataProp="#xxx" data-anotherDataProp="#yyy" onclick="actProp(this)">

Open in new window

Avatar of Alex Lord
Alex Lord

ASKER

Hey leak

i implemented your tip and it sounds very clear but the result wasnt the exptect outcome.

below is the out come

["POSTITION"]
contact.php?cid=10296&id=:3330 (2) ["FIRST_NAME", "FIRST_NAME"]

Open in new window


it should be more like ['POSITION', 'FIRST_NAME']
hey Chris Stanyon

Sorry here is how it is declared -

var propCheck = $('#default-checked-list').find('input').each(function(){});
    

    
      
    propCheck.on('change', actProp);

Open in new window


within that container is a number of checkboxes.

if you are referring to
   var propChk = "#" + $(this).attr('name');
     var chkCon = "#" + $(this).attr('class');

Open in new window


this is used the another output by this function.

but i shall give ur tip a go.
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris Stanyon declaring the array outside the function seemed to have done the trick.
Hey Chris Stanyon

the reason for that is those two will handle another event down the line which needs the values from class and name.
No worries Alex. Glad you got it working.

FYI - you can simplify the event binding:

$('#default-checked-list').find('input').change(actProp);