Search code examples
javascriptjqueryasp.netonclickfieldset

Fieldset onclick getting radio button clicked value


I have a fieldset in my HTML which basically represents 5 star rating system like following:

<fieldset class="rating" id="ratingSystem">
    <input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>

And I've defined an onclick event which defines when user clicks on some of the star ratings (radio buttons) then I'd simply for testing purposes display the clicked value, but I always get double values displayed instead of the displayed one...

For instance if I clicked on 5 I get displayed in console:

undefined  
5

Second time I click on 4 I get values:

5
4

The code I use for it is:

$('#ratingSystem').click(function () {
        console.log($('input[name=rating]:checked').val());
    });

What am I doing wrong here?

Edit, these are the CSS classes that I use to turn the radio buttons into stars so that it looks like star rating system:

fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }

/****** Style Star Rating Widget *****/

.rating { 
  border: none;
  float: left;
}

.rating > input { display: none; } 
.rating > label:before { 
  margin: 5px;
  font-size: 1.25em;
  font-family: FontAwesome;
  display: inline-block;
  content: "\f005";
}

.rating > .half:before { 
  content: "\f089";
  position: absolute;
}

.rating > label { 
  color: #ddd; 
 float: right; 
}

/***** CSS Magic to Highlight Stars on Hover *****/

.rating > input:checked ~ label, /* show gold star when clicked */
.rating:not(:checked) > label:hover, /* hover current star */
.rating:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */

.rating > input:checked + label:hover, /* hover current star when changing rating */
.rating > input:checked ~ label:hover,
.rating > label:hover ~ input:checked ~ label, /* lighten current selection */
.rating > input:checked ~ label:hover ~ label { color: #FFED85;  } 

Could any of these classes be the source of the issue?


Solution

  • You may attaching another event to the checkbox's somewhere else but you could check the working snippet below based on your OP code shows that the console.log fire just one time on click.

    I suggest to attach the click to the input directly the get the current checked value using just :

    $(this).val();
    

    Hope this helps.

    $('#ratingSystem input').click(function () {
      console.log($(this).val());
    });
    fieldset, label { margin: 0; padding: 0; }
    body{ margin: 20px; }
    h1 { font-size: 1.5em; margin: 10px; }
    
    /****** Style Star Rating Widget *****/
    
    .rating { 
      border: none;
      float: left;
    }
    
    .rating > input { display: none; } 
    .rating > label:before { 
      margin: 5px;
      font-size: 1.25em;
      font-family: FontAwesome;
      display: inline-block;
      content: "\f005";
    }
    
    .rating > .half:before { 
      content: "\f089";
      position: absolute;
    }
    
    .rating > label { 
      color: #ddd; 
      float: right; 
    }
    
    /***** CSS Magic to Highlight Stars on Hover *****/
    
    .rating > input:checked ~ label, /* show gold star when clicked */
    .rating:not(:checked) > label:hover, /* hover current star */
    .rating:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */
    
    .rating > input:checked + label:hover, /* hover current star when changing rating */
    .rating > input:checked ~ label:hover,
    .rating > label:hover ~ input:checked ~ label, /* lighten current selection */
    .rating > input:checked ~ label:hover ~ label { color: #FFED85;  } 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <fieldset class="rating" id="ratingSystem">
      <input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
      <input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
      <input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
      <input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
      <input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
    </fieldset>