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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:58:25+00:00 2026-06-15T13:58:25+00:00

I am creating a review-control and using radiobutton for grade selection. I wrote some

  • 0

I am creating a review-control and using radiobutton for grade selection. I wrote some simple js to add a diffrent class when a radiobutton is checked.

The problem is that you can check every radio button, I want the user only to check one value. I writing this in javascript, but jQuery is welcome or a smartare solution.

Demo : http://jsfiddle.net/cbqt8/5/

HTML:

<div class="reviews">
<label class="input-check">
  <input onchange="change_state(this)" type="radio" value="1"   name="review[rating]" /> Bad
</label>
<label class="input-check">
  <input onchange="change_state(this)" type="radio" value="2"   name="review[rating]" /> Its okey
</label>
<label class="input-check">
  <input onchange="change_state(this)" type="radio" value="3"   name="review[rating]" /> Great
</label>
<label class="input-check">
  <input onchange="change_state(this)" type="radio" value="4"   name="review[rating]" /> Awesome
</label>
<label class="input-check">
  <input onchange="change_state(this)" type="radio" value="5"   name="review[rating]" />Super
</label>
</div>

JavaScript:

function change_state(obj) {
    if (obj.checked) {
        //if radiobutton is being checked, add a "checked" class
        obj.parentNode.classList.add("checked");
    }
    else {
        //else remove it
        obj.parentNode.classList.remove("checked");
    }
}

CSS:

/*reviews box*/
.reviews{
    padding: 25px;
    margin:0;
}
/*this is the style of an radio "button"*/
.input-check {
    display: inline-block;
    height:20px;
    padding:5px 8px;
    background:green;
    width:90px;
    color:white;
    text-align: center;
}
/* This is the style for a radio "button" */
.input-check.checked{
    background:red;
    color:black;
    font-weight:bold;
}
/*Hide the radiobutton*/
.input-check input {
    display:none;
}
  • 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-15T13:58:28+00:00Added an answer on June 15, 2026 at 1:58 pm

    You got to remove the checked class for previously checked item.

    jsFiddle: http://jsfiddle.net/P8jB8/

    function change_state(obj) {
    
        if (obj.checked) {
            var checkedNodes = getElementsByClassName(document, "checked");
            for (var i=0;i<checkedNodes.length;i++) {
                checkedNodes[i].classList.remove("checked");
            }
    
            //if radiobutton is being checked, add a "checked" class
            obj.parentNode.classList.add("checked");
        }
        else {
            //else remove it
            obj.parentNode.classList.remove("checked");
        }
    }
    
    function getElementsByClassName(node,classname) {
      if (node.getElementsByClassName) { // use native implementation if available
        return node.getElementsByClassName(classname);
      } else {
        return (function getElementsByClass(searchClass,node) {
            if ( node == null )
              node = document;
            var classElements = [],
                els = node.getElementsByTagName("*"),
                elsLen = els.length,
                pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
    
            for (i = 0, j = 0; i < elsLen; i++) {
              if ( pattern.test(els[i].className) ) {
                  classElements[j] = els[i];
                  j++;
              }
            }
            return classElements;
        })(classname, node);
      }
    }
    

    ​

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

Sidebar

Related Questions

Creating a simple RPG game, first time using XNA. Trying to get my character
I wanted to get a code review of my methods for creating a simple
I'm creating a simple ajax call to retrieve a list of user reviews. I
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Error creating bean with name 'sessionFactory' defined in class path resource [ApplicationContext.xml]: Invocation of
I have a slight problem with creating a button on the server side code.
I am creating a website using master page, which works fine. but after I
I am creating thumbnails for online images using: public static string SaveThumbnail(string imageUrl, int
I'm creating a kind of wizard using jQueryUI tabs. The first tab has the
I'm quite new to creating layout using SWT. This is what I have to

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.