Solved

javascript function giving wrong output

Posted on 2012-03-20
9
290 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now