cofactor
asked on
javascript function giving wrong output
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
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
All of your checkboxes have name="student" making them mutually exclusive. When you check the next one, the last one becomes unchecked.
ASKER
>>>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.
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.
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']" ).serializ eArray();
The * will find any name attribute that contains the word "Student" and add them to the array.
var fields = $("input[name*='Student']"
The * will find any name attribute that contains the word "Student" and add them to the array.
ASKER
>>>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.
No.
I just want to collect all the checkbox ID's selected by user.
My bad. I'm thinking radio buttons.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>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 ?
why there is no pop ? what if the user first select a checkbox and then again unselect right away.... do you consider this situation ?
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.
ASKER
Thanks. That solved my problem.