Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript function giving wrong output

Posted on 2012-03-20
9
Medium Priority
?
300 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
Industry Leaders: 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!

 

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 2000 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

Independent Software Vendors: 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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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 …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

971 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