2

I am having problem with my jquery code, I am trying to state that if "Yes" button is on, disable the "No" button, else if the "No" button is on, then disable the "Yes" button, else enable both buttons.

But at the moment do button is being disabled when the other button is turned on, does anyone know why this is?

Below is code:

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

$(btn).toggleClass("answerBtnsOff");
$(btn).toggleClass("answerBtnsOn");

    if ($("#answerYes", context).hasClass('.answerBtnsOn')) {
        $("#answerNo", context).attr("disabled", "disabled");
    }

    else if ($("#answerNo", context).hasClass('.answerBtnsOn')) {
        $("#answerYes", context).attr("disabled", "disabled");
    }

    else {
    $("#answerYes", context).removeAttr("disabled");
    $("#answerNo", context).removeAttr("disabled");
    }


    return false;
}

below is html for both yes and no buttons:

Yes button:

<input class="answerBtns answers answerBtnsOff" name="answerYesName"   id="answerYes"   type="button"   value="Yes"     onclick="btnclick(this);"/>

No Button:

<input class="answerBtns answers answerBtnsOff" name="answerNoName"    id="answerNo"        type="button"   value="No"      onclick="btnclick(this);"/>
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
user1490145
  • 153
  • 10
  • 1
    How is this question any different than your [other](http://stackoverflow.com/questions/11379865) [questions](http://stackoverflow.com/questions/11417414) [recently](http://stackoverflow.com/questions/11423744) on [StackOverflow](http://stackoverflow.com/users/1490145/user1490145?tab=questions) – Metro Smurf Jul 11 '12 at 00:14
  • Oh I did not know about these questions, there is a group of us and we use this account to ask any programming questions we may have, some of our group members must of asked some questions related to this application, but this question is asking how to disable and undisable buttons – user1490145 Jul 11 '12 at 00:19
  • Isn't this type of thing what radio buttons are for? If you want to use push buttons wouldn't it be better to make it that clicking "Yes" automatically deselects "No" rather than disabling it? Otherwise how does the user change their mind? – nnnnnn Jul 11 '12 at 00:20
  • @nnnnnn That is a good idea, thanks – user1490145 Jul 11 '12 at 00:22

2 Answers2

2

You dont put the . in toggleClass/hasClass/removeClass/... you only use them in selectors

Musa
  • 89,286
  • 16
  • 105
  • 123
0

Change this .attr("disabled", "disabled"); to this .attr("disabled", true); for both buttons. Plus, you will want to remove the period like @Musa said

JoeCortopassi
  • 5,023
  • 6
  • 35
  • 64