Incorrect number is displayed (Jquery)

In my application I have anumber of Answers Textbox. When the user selects all of the buttons by clicking on the "Select All Answers" button, it will automatically display a number in the textbox counting up all of the selected buttons (all the buttons turn green. But I have a problem which can be seen easily if you follow the steps below:

Follow steps below in my jsfiddle:

1. Open the Grid and select option "5". Buttons A to E would appear. Number 5 is displayed in the number of answers textbox which is correct.
2. Click "Select All Answers" buttons. All buttons A to E would be selected (turn green) and the "Number of Answers" textbox would display the number 5.
3. Click the "Add question" button, your details are added into a new row below.
4. Now go back to the top control and Open the Grid again and select option "3". As you can see buttons A to C would appear and are all selected (turn green). Yet the textbox displays the number 8. This should display number 3 as three buttons in the top control are selected. The reason it is displaying 8 is because it is counting of all of the green selected buttons from the top control and the row added. They should be seperated so that if all buttons are selected from top, it should only count up all the selected buttons in the top control only and not any of the buttons in the added rows.

This line of code you need to look at is below
 $('.numberAnswerTxt').val($('.answerBtnsOn:visible').length)

Open in new window


code in jsfiddle, click here
u0867587Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcnuteCommented:
Hello,

use the parent > child selector of jquery, to restrict the counting of the line like this:

 $('.numberAnswerTxt').val($('.optionTypeTbl >.answerBtnsOn:visible').length)
0
u0867587Author Commented:
Hi,

The only issue when I include you code is that if I select option 5, click on "Select All Buttons" Button and the change the option to 4, except displaying 4 in the textbox (as it displays 4 buttons which are all selected), it displays 0 instead in the textbox. Do you know why this is?
0
mcnuteCommented:
   var context = $('#optionAndAnswer');
         $('.numberAnswerTxt', context).val(context.find('.answerBtnsOn').length);

try with this code instead of the line told you before.
You want to count the buttons with the On class anyways. Counting the visible Buttons will give you the green and white ones together.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
u0867587Author Commented:
Thank you for your help. It has answer my question perfectly :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.