Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript function giving wrong output

Posted on 2012-03-20
9
Medium Priority
?
298 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

704 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