0

I am using jquery raty (a rating script). I have multiple rating options and if none of them are selected the hidden post field that is generated doesnt exist. This is how I was able to name and pull the score selected for all 4 options.

<section>
<label>Option 1:</label>
<div class="options" data-score-name="option1"></div>
</section>

<section>
<label>Option 2:</label>
<div class="options" data-score-name="option2"></div>
</section>

<section>
<label>Option 3:</label>
<div class="options" data-score-name="option3"></div>
</section>

<section>
<label>Option 4:</label>
<div class="options" data-score-name="option4"></div>
</section>


<script type="text/javascript">
$('.options').raty({
  half: true,
  width: 'width: 150',
  path: '/assets/img',
  starOff: 'star-off.png',
  starOn: 'star-on.png',
  starHalf: 'star-half.png',
  readOnly: false,
  scoreName: 'score[]',

  click: function(score)
  {
     var ScoreName = $(this).attr('data-score-name');
     $(this).append("<input type='hidden' name='rate[" + ScoreName + "]' value='" + score + "'>");
  }

});
</script>

The problem i'm having is , how do i make each option have a default value if its not selected? I'm not sure if there is a onload option that will give it a default value and was only able to use onclick.

Exploit
  • 6,278
  • 19
  • 70
  • 103

1 Answers1

0

Figured it out and works like a charm. Might be the wrong way of doing it but it works :)

<script type="text/javascript">
$('.options').raty({
  half: true,
  width: 'width: 150',
  path: '/assets/img',
  starOff: 'star-off.png',
  starOn: 'star-on.png',
  starHalf: 'star-half.png',
  readOnly: false,
  scoreName: 'score[]',
  score: function() {
    var ScoreName = $(this).attr('data-score-name');
    return $(this).append("<input type='hidden' name='rate[" + ScoreName + "]' class='" + ScoreName + "' value='0'>");
  },



  click: function(score)
  {
     var ScoreName = $(this).attr('data-score-name');
     $("." + ScoreName).val(score);
     //$(this).append("<input type='hidden' name='rate[" + ScoreName + "]' value='" + score + "'>");
  }

});
</script>
Exploit
  • 6,278
  • 19
  • 70
  • 103