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 7787627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:43:54+00:00 2026-06-01T20:43:54+00:00

I need to add three more buttons to this script for a total of

  • 0

I need to add three more buttons to this script for a total of 5.

I first made the script for two buttons, but I now need to add three more.

Would I only be concerned with this portion? Is this the only area I need to change?

    var varval = '';
    if ($(this).hasClass('button-toggle-on')) {
        varval = $(this).hasClass('gnF') ? 'Female' : 'Male';

Link: http://jsfiddle.net/9cr4F/9/

Here is my script:

$(function() {
var $buttons = $("input[type='button']");
$buttons.click(function() {

    $(this).parent().siblings('.bx, #bxGender .gender').css({'background':'#2F2F2F','color':'#fff'});
    $buttons.not(this).removeClass('button-toggle-on');
    $(this).toggleClass('button-toggle-on').attr('style','');

    var varval = '';
    if ($(this).hasClass('button-toggle-on')) {
        varval = $(this).hasClass('gnF') ? 'Female' : 'Male';

    $(this).siblings('input[type="button"]').css('background-position','0 -180px');
    }
    else {
          $(this).siblings('input[type="button"]').attr('style','');
       $(this).parent().siblings('.bx').attr('style',''); 
    }
    $("#gender").val(varval);
});
});​
  • 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-06-01T20:43:56+00:00Added an answer on June 1, 2026 at 8:43 pm

    From what I’m getting from your code, you have two buttons right now: Male and Female. When you click one, it changes some #gender element’s value to either Male or Female, depending on whether or not it has some class.

    I imagine that you want to add three more possible values to gender, hence the three new buttons. If that’s the case, then there’s a critical place in your code that you will have to modify:

    varval = $(this).hasClass('gnF') ? 'Female' : 'Male';
    

    Right now, you’re using a ternary operator to determine the correct value. That’s going to get dirty if you’re looking at five possible values instead of the original two.

    Barring any changes in your HTML, I’d suggest you use a switch instead.

    But regarding your real question is that the only thing I need to change?, I’d have to say that from the looks of it, you can really make your code so much simpler if you set up your HTML right.

    Something like:

    <input type="button" data-gender="Male" />
    <input type="button" data-gender="Female" />
    

    … makes it so much easier to just do this instead:

    var varval = $(this).attr('data-gender');
    

    Best of all, that works, no matter how many buttons you have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cool script that works with two buttons. I need to add
I'm using the Composite Application Library with Silverlight, and I need to add three
I need add a new user group for mediawiki. The new group has more
I need to add a dependency on a specific version of GDIPlus. This is
For example I have long list of buttons: <input type=button name=clickbutton onclick='dosomething(this)'> But instead
I am enhancing an existing JSF app. There I need to add ExtGWT component
need to add a 2nd css stylesheet to a page. do i add a
I need to add an invite users functionality to my social networking application so
I need to add CRRedist2008_x86.msi to my deployment project as it is a requirement
I need to add css height to each page dynamically with jQuery. So far

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.