javascript function giving wrong output

Here is my file attached.  Please  unzip to  find test.html .  run this in IE browser.

In test.html  page , I select a checkbox in page1 and then go to next page .  I select another checkbox in page2  and then go to next page. I select another checkbox there in page3.

Now click submit button.

I get an alert "1 item selected".
This is wrong. It should be "3 item selected"  because I checked 3 checkboxes.

Could you please take a look whats wrong in my javascript function ? how do I fix it ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
All of your checkboxes have name="student" making them mutually exclusive. When you check the next one, the last one becomes unchecked.
cofactorAuthor Commented:
>>>All of your checkboxes have name="student" making them mutually exclusive.

OK.  thanks but  I dont want to keep separate names for checkboxes.   I have kept separate ID's for them though.

Can we modify the javascript function  now  so that it can  give the correct results ?

Basically ,  at the end I  want to send the selected checkbox ID's  to the server.
Tom BeckCommented:
Are you trying to make the checkboxes on each page mutually exclusive? If so, you can make the name for the inputs on each page unique. Like: page one name="student1", page two name="student2". Then in jQuery:

var fields = $("input[name*='Student']").serializeArray();

The * will find any name attribute that contains the word "Student" and add them to the array.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

cofactorAuthor Commented:
>>>Are you trying to make the checkboxes on each page mutually exclusive?


I  just want to collect  all the checkbox  ID's   selected by user.
Tom BeckCommented:
My bad. I'm thinking radio buttons.
Tom BeckCommented:
How about this? Add this to each checkbox:  onclick="adjustCount(this,;"

<script type="text/javascript" id="js1">
function onSubmit() 
  if (fields.length == 0) 
    alert('nothing selected'); 
    alert(fields.length + " items selected"); 
var fields = new Array();
function adjustCount(chkBox, chkBoxID){
        var tempArray = new Array();
        for(i = 0; i<fields.length; i++){
            if(fields[i] != chkBoxID){
        fields = tempArray;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofactorAuthor Commented:
>>How about this? Add this to each checkbox:  onclick="adjustCount(this,;"

why there is no  pop ?  what if the user first select a checkbox and then again unselect right away.... do you consider this  situation ?
Tom BeckCommented:
I did consider that situation. The "else" section of the adjustCount function loops through the array and removes any checkbox id where the checkbox is not checked. Array.pop will not work because it only removes the last item in the array. I cannot be certain that the checkbox just unchecked is the last item in the array.
cofactorAuthor Commented:
Thanks. That solved my problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.