0

I want to create a star rating system for a person, for this user inputs value for five different categories and I want to show the average of those values on a group of radio buttons. I have obtained the average, but now not getting how to display the average.

$("input[name='overall']").each(function(){
                    if(this.value == response.average){
                        $(this).attr('checked', true); 
                    }
                });

I want to display the average over this field----

<div id="overall_rating" class="Clear">
<input id="overall" name="overall" type="radio" class="star {split:5} required" disabled="disbled" value="0.2" />
<input id="overall" name="overall" type="radio" class="star" value="0.4"/>
<input id="overall" name="overall" type="radio" class="star" value="0.6"/>
<input id="overall" name="overall" type="radio" class="star" value="0.8"/>
<input id="overall" name="overall" type="radio" class="star" value="1.0"/>
<input id="overall" name="overall" type="radio" class="star" value="1.2"/>
<input id="overall" name="overall" type="radio" class="star" value="1.4"/>
<input id="overall" name="overall" type="radio" class="star" value="1.6"/>
<input id="overall" name="overall" type="radio" class="star" value="1.8"/>
<input id="overall" name="overall" type="radio" class="star" value="2.0"/>
<input id="overall" name="overall" type="radio" class="star" value="2.2"/>
<input id="overall" name="overall" type="radio" class="star" value="2.4"/>
<input id="overall" name="overall" type="radio" class="star" value="2.6"/>
<input id="overall" name="overall" type="radio" class="star" value="2.8"/>
<input id="overall" name="overall" type="radio" class="star" value="3.0"/>
<input id="overall" name="overall" type="radio" class="star" value="3.2"/>
<input id="overall" name="overall" type="radio" class="star" value="3.4"/>
<input id="overall" name="overall" type="radio" class="star" value="3.6"/>
<input id="overall" name="overall" type="radio" class="star" value="3.8"/>
<input id="overall" name="overall" type="radio" class="star" value="4.0"/>
<input id="overall" name="overall" type="radio" class="star" value="4.2"/>
<input id="overall" name="overall" type="radio" class="star" value="4.4"/>
<input id="overall" name="overall" type="radio" class="star" value="4.6"/>
<input id="overall" name="overall" type="radio" class="star" value="4.8"/>
<input id="overall" name="overall" type="radio" class="star" value="5.0"/>
</div>

Please help me find the solution to this problem.

5
  • 2
    You can't have multiple elements with the same id (probably not what's causing the issue, but you can't)
    – bfavaretto
    Aug 28, 2012 at 13:25
  • Are you sure response.average is available in that context?
    – bfavaretto
    Aug 28, 2012 at 13:26
  • try using .prop() rather than .attr() to check the radio button...
    – maxedison
    Aug 28, 2012 at 13:29
  • What do you mean "display the average over this field?" Aug 28, 2012 at 13:31
  • Your code should be working, if response.average is really available: jsfiddle.net/eT5VF
    – bfavaretto
    Aug 28, 2012 at 13:36

2 Answers 2

1

I hope this code help you.

var average = response.average;

$("input[value='" + average + "']").attr("checked", "checked");

You just need to check if the response.average is contained in value attribute of one radio button.

0

All of your input fields have the same ID. IDs have to be unique on a web page. Try correcting that first (i.e., remove the IDs from the inputs as you don't need them here).

Then try $(this).prop("checked", true);

2
  • TypeError: $(this).prop is not a function $(this).prop("checked", true);
    – awsumnik
    Aug 28, 2012 at 14:08
  • Thanks everybody for the help. Actually the value of response.average was of string type, I have parsed that to float type and now the code is working. Thanks.
    – awsumnik
    Aug 28, 2012 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.