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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:14:14+00:00 2026-06-02T05:14:14+00:00

I have a cool script that works with two buttons. I need to add

  • 0

I have a cool script that works with two buttons. I need to add THREE more buttons as options for a total of FIVE altogether.

Does the whole script have to be re-written or could I just add more DIV classes? Not sure how to approch. Working for days on this…

Here is my link: http://jsfiddle.net/9cr4F/9/

HTML:

<div id="r6">    

<div class="bx bx1">6</div>
<div id="sp"></div>
<div class="bx bx2">Gender</div>
<div id="sp"></div>
<div id="bxGender">
<input type="button" class="gnM" >
<div id="sp"></div>
<input type="button" class="gnF">
<div id="sp"></div>
<input class="req-string gender"  id="gender" name="gender"></div>

</div>

CSS:

#r6 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
}

.bx {
    height:60px;
    line-height:60px;
    float:left;
    font-size:105%;
    margin-top:15px;
    color: #000;
    background-color: #E8E8E8
}
.bx1 {
    width:60px;
    text-align:center
}
.bx2 {
    width:175px;
    padding-left:20px
}
#sp {
    width:  15px;
    height:60px;
    float:left;
}
.gnM {
    width:137px;
    height: 60px;
    background:#E8E8E8 url('http://www.41q.org/admin/img/male.png') 0px 0px no-repeat;
    margin-top:0px;
    padding: 0;
    border: 0;
    margin-left: 0px;
    float: left;
    outline: none;
    text-align:center;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 105%;
    font-style:normal;
    color:#03F;
}
.gnF {
    width:137px;
    height: 60px;
    background:#E8E8E8 url('http://www.41q.org/admin/img/female.png') 0px 0px no-repeat;
    margin-top:0px;
    padding: 0;
    border: 0;
    margin-left: 0px;
    float: left;
    outline: none;
    text-align:center;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 105%;
    font-style:normal;
    color:#03F;
}
#r6 #bxGender .button-toggle-on {
    background-position: 0 -120px
}
#bxGender {
    width:486px;
    height:60px;
    float:left;
    margin-top:15px
}

#r6:hover div.bx, #r6:hover input {background-color: #A9A9A9; cursor:pointer}
#r6:hover .gnM, #r6:hover .gnF {background-position: 0 -60px; cursor:pointer}

jQuery:

$(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-02T05:14:15+00:00Added an answer on June 2, 2026 at 5:14 am

    First thing you need is a markup to validate, at the moment it is not, IDs in a document must be UNIQUE so when you do

    <div id="sp"></div>
    

    you should do

    <div class="sp"></div>
    

    and try to get the clone nicely nested, even if it copies badly in here sometimes it helps to even have just few spaces here and there 🙂

    In your CSS don’t repeat the same properties if it applies to multiple elements, for example I restructured your gnF and gnM class, so all the properties are together and only the background image property is seperate.

    .gnM, 
    .gnF, 
    .gnA {
        background:#E8E8E8 0px 0px no-repeat; 
        text-indent:-10000px;
        width:137px;
        height: 60px;
        margin-top:0px;
        padding: 0;
        border: 0;
        margin-left: 0px;
        float: left;
        outline: none;
        text-align:center;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 105%;
        font-style:normal;
        color:#03F;
    }
    .gnA {
        background-image:url('http://www.41q.org/admin/img/alien.png');   
    }
    .gnM {
        background-image:url('http://www.41q.org/admin/img/male.png');   
    }
    .gnF {
        background-image:url('http://www.41q.org/admin/img/female.png');
    }
    

    You can simplify the jquery side by using the value attribute on your button, if you set a text-indent then the text wont show, and then you can simply retrieve the value using $(this).val();

    $(function() {
    
        var $buttons = $("input[type='button']"),
            varval;
    
        $buttons.click(function() {
    
            // this should be done using a class to keep the js cleaner
            $(this).parent().siblings('.bx, #bxGender .gender').css({
                'background': '#2F2F2F',
                'color': '#fff'
            });
    
            $buttons.removeClass('button-toggle-on');
            $(this).addClass('button-toggle-on');
    
            $("#gender").val($(this).val());
        });
    });​
    

    This gives you a lot more flexibility as you can have as many button as you want but if you are building a form with multiple rows, as it seems, I would suggest also using a class for the input that contains the result and modify the value assignment so that it grabs the closest result input and update that, so that way it works for all rows.

    Here is the modified fiddle http://jsfiddle.net/9cr4F/14/ – the third buttons styling is screwed of course but I’m sure it gives you what you need 🙂

    Edited: Optimized CSS a bit more
    Edited 2: Optimized the jquery code and move the background positioning to the class button-toggle-on as it is on when the background position need to be applied.

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

Sidebar

Related Questions

I have a cool snippet of code that works well, except one thing. The
I have a share backup bash script that works as is, but it uses
Does anyone have a suggestions for a superb cool image gallery that I can
ASP.Net MVC3 is cool and all but I have this question more out of
To index my website, I have a Ruby script that in turn generates a
I have cool uri in format page.com/par1/par2/par3 But on this page is form, that
I have the following question: I am working on a JQuery script that turns
So I have some cool Image Processing algorithm. I have written it in OCaml.
Just writing some CSS and I have a pretty cool Search form where you
hi am new to the jQuery lib and so far very cool i have

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.