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);"/>
4
  • 1
    How is this question any different than your other questions recently on StackOverflow Commented Jul 11, 2012 at 0: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 Commented Jul 11, 2012 at 0: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? Commented Jul 11, 2012 at 0:20
  • @nnnnnn That is a good idea, thanks Commented Jul 11, 2012 at 0:22

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

are you mentioning .prop("disabled", true);? stackoverflow.com/a/1414366/17447

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.