Solved

javascript function giving wrong output

Posted on 2012-03-20
9
289 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 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

12 Experts available now in Live!

Get 1:1 Help Now