1
\$\begingroup\$

My tooltip works fine, but I need to simplify this code.

var genderSelect = {
 getGenderSelect: function () {
 if(this.val('Girl')){
 $(this).find('#girl-character').show();
 $(this).find('#boy-character').hide();
 }else if(this.val('Boy')){
 $(this).find('#boy-character').show();
 $(this).find('#girl-character').hide();
 }
 },
 init: function(){
 $('.control-label input[name="gender"]').change(
 $('#caracters').show();
 genderSelect.getGenderSelect();
 )};
};
genderSelect.init();
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 3, 2015 at 9:39
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question. \$\endgroup\$ Commented Nov 3, 2015 at 9:41
  • \$\begingroup\$ If #girl-character is unique on the page (and it should be given that it is an id), you don't need $(this).find('#girl-character'), just do $('#girl-character'). Likewise for #boy-character. \$\endgroup\$ Commented Nov 3, 2015 at 10:33

1 Answer 1

1
\$\begingroup\$

You can shorten the getGenderSelect function like this:

var genderSelect = {
 getGenderSelect: function () {
 $(this).find('#girl-character').toggle(this.val() == 'Girl');
 $(this).find('#boy-character').toggle(this.val() == 'Boy'); 
 },
 init: function(){
 $('.control-label input[name="gender"]').change(
 $('#caracters').show();
 genderSelect.getGenderSelect();
 )};
};
genderSelect.init();

The toggle function takes a boolean which determines whether to show or hide the selected elements.

answered Nov 3, 2015 at 9:49
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.