Toggle Yes / NO with multiple questions

后端 未结 3 550
太阳男子
太阳男子 2020-12-20 03:49

I have 5 questions in a form, and a few of them have a YES / NO response. I wanted to just be able to toggle the yes and no buttons once the user selected one of them. The i

3条回答
  •  余生分开走
    2020-12-20 04:25

    You can use .closest('div') to target the closest div of the element that triggered the .change() event and use .find() to target the class.

    One function to to deal with all change events for elements with the class of MyToggle

    This will allow you to run the same function for all questions as shown in the demo below.

    Update: Yes/No toggle

    Demo

    $(".MyToggle").click(function() {
        $(this).val()=='yes'?$(this).closest('div').find('.Questions').show():$(this).closest('div').find('.Questions').hide();
    });
    .Questions{display:none;}
    
    
    Yes
    Questions?
    Yes No
    Questions?
    Yes No
    Questions?

    I hope this helps. Happy coding!

提交回复
热议问题