Solved

javascript function giving wrong output

Posted on 2012-03-20
9
292 Views
Last Modified: 2012-03-22
Here is my   test.zip 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 ?
test.zip
0
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37741909
All of your checkboxes have name="student" making them mutually exclusive. When you check the next one, the last one becomes unchecked.
0
 

Author Comment

by:cofactor
ID: 37741959
>>>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.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37741971
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.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:cofactor
ID: 37741993
>>>Are you trying to make the checkboxes on each page mutually exclusive?

No.

I  just want to collect  all the checkbox  ID's   selected by user.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37742004
My bad. I'm thinking radio buttons.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 37742210
How about this? Add this to each checkbox:  onclick="adjustCount(this,this.id);"

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

Open in new window

0
 

Author Comment

by:cofactor
ID: 37743615
>>How about this? Add this to each checkbox:  onclick="adjustCount(this,this.id);"

why there is no  pop ?  what if the user first select a checkbox and then again unselect right away.... do you consider this  situation ?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37743822
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.
0
 

Author Comment

by:cofactor
ID: 37751392
Thanks. That solved my problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question