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?
 
mcnuteConnect With a Mentor Commented:
   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
 
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
 
u0867587Author Commented:
Thank you for your help. It has answer my question perfectly :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.