Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:33:30+00:00 2026-05-27T17:33:30+00:00

I have a textbox which should state how many buttons are selected. The problem

  • 0

I have a textbox which should state how many buttons are selected.

The problem I have is that what happens is that the user selects there buttons and then they add those buttons in a row. Now if I select the next set of buttons, what it is doing is that it adding the previously selected buttons with the current selected buttons with.

For example is user selected 2 buttons and then added it into a new, then if the user selects another 2 buttons, it should state that 2 buttons are selected but instead it states buttons a selected.

Now the reason for this is that I have multiple textboxes with the same class (.numberAnswerTxt), so that is why I think it is adding the numbers together. So what my question is that how can I differenticate between the textboxes with the same class.

There is one textbox on top where the user selctes the buttons and then there are textboxes for each row stating the amount of buttons previously selected for each row.

Below is the jquery code which displays the number of buttons selected in the textbox:

$('.numberAnswerTxt').val($('.answerBtnsOn:visible').size());

Below is the jquery code for the new rows added each time:

 $('.numberAnswerTxt', context).each(function() {
        var $this = $(this);
        var $noofanswersText = $("<input type='text' class='numberAnswerTxt' onkeyup='numberKeyUp(this)' onkeypress='return isNumberKey(event)' onChange='getButtons()'>").attr('name', $this.attr('name')).attr('value', $this.val())

        $noofanswers.append($noofanswersText);
    });

Below is html of first textbox:

<input type="text" name="numberAnswer" class="numberAnswerTxt" onkeyup="numberKeyUp(this)" onkeypress="return isNumberKey(event)" onChange="getButtons()" >

html of the buttons they are selecting:

<input class="answerBtns answers" name="answerAName"    id="answerA"        type="button"   value="A"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerBName"    id="answerB"        type="button"   value="B"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerCName"    id="answerC"        type="button"   value="C"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerDName"    id="answerD"        type="button"   value="D"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerEName"    id="answerE"        type="button"   value="E"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerFName"    id="answerF"        type="button"   value="F"       onclick="btnclick(this);"/>
<input class="answerBtns answers" name="answerGName"    id="answerG"        type="button"   value="G"       onclick="btnclick(this);"/>

onlick event code:

function btnclick(btn)
{
    var context = $(btn).parents('#optionAndAnswer');
    if (context.length == 0) {
        context = $(btn).parents('tr');
    }

    if (context.find(".numberAnswerTxt").val() == "") {
        alert('You must first enter in the number of answers you require in the textbox above');
        return false;
    }

    if ($(btn).hasClass("answerBtnsOn")) {
        $(btn).removeClass("answerBtnsOn").addClass("answerBtnsOff");
        return false;
    }

    var currenttotal = context.find('.answerBtnsOn').length;
    if (context.find(".numberAnswerTxt").val() <= currenttotal) {
        alert('You are not allowed beyond the limit of the number of answers you require, deselect other button');
        return false;
    }

    if ($(btn).hasClass("answerBtnsOff")) {
        $(btn).removeClass("answerBtnsOff").addClass("answerBtnsOn");
        return false;
    }
}

Below is the function where a new row is added each time the user submits the number of buttons they have selected:

function insertQuestion(form) {   


    var $tr = $("<tr></tr>");
    var $noofanswers = $("<td class='noofanswers'></td>");
    var $answer = $("<td class='answer'></td>");


   $('.numberAnswerTxt', context).each(function() {
        var $this = $(this);
        var $noofanswersText = $("<span class='oneanswer'>Only 1 Answer</span><input type='text' class='numberAnswerTxt' onkeyup='numberKeyUp(this)' onkeypress='return isNumberKey(event)' onChange='getButtons()'>").attr('name', $this.attr('name')).attr('value', $this.val())

        $noofanswers.append($noofanswersText);
    }); 


    $('#optionAndAnswer .answers').each(function() {
        var $this = $(this);
        var $newBtn = '';
        if($this.is(':visible')){
            $newBtn = $("<input class='answerBtnsRow answers' type='button' style='display: inline-block;' onclick='btnclick(this);' />").attr('name', $this.attr('name')).attr('value', $this.val()).attr('class', $this.attr('class'));
        }else{
            $newBtn = $("<input class='answerBtnsRow answers' type='button' style='display: none;' onclick='btnclick(this);' />").attr('name', $this.attr('name')).attr('value', $this.val()).attr('class', $this.attr('class'));
        }

        $answer.append($newBtn);
    });


    $tr.append($noofanswers);
    $tr.append($answer);

    $('#qandatbl').append($tr);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:33:30+00:00Added an answer on May 27, 2026 at 5:33 pm

    I suppose you also have some sort of submit button.
    when you click on that button you have to reset all buttons to their default values.
    Class etc. I think that is whats going wrong.

    Just assuming you put your buttons in a div, not together with the submit button ofcourse.
    try this.

    $('#myDiv input[type=button]').each(function(){
    //if you dont say what class to remove it will just remove any class on it.
    $(this).removeclass();
    $(this).addclass('yourDefaultClass');
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextBox (which shows notes). Now the user Selects his/her name and
Here i have a textbox in which user inputs html tags like <h1>hello</h1> then
I have a textbox which should allow user to enter only positive or negative
I have a web form where I have a textbox in which the user
what i have is a textbox into which the user types a string. The
I have using c#.net. I have a textbox which should contain a time. It
I have a textbox in which the user can edit text, in a scripting
jquery, textbox with autocomplete, change event problem: I have a textbox which is used
I have textbox which should be used to send formatted text to remote application,
I have a TextBox which should only accept numbers (they can be simple int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.