javiertoledo / bootstrap-rating-input

Another plugin that eases the generation of rating stars for jQuery and Bootstrap
MIT License
319 stars 155 forks source link

Setting value does not display correct rating (i.e. number of stars) #16

Open wloescher opened 10 years ago

wloescher commented 10 years ago

I'm using your fine little piece of code in a Bootstrap 3 modal pop up. I'm setting the value of the control, and it appears to get set correctly. $("#rating").val(3); alert($("#rating").val()); // 3

However, no stars are selected when the controls is rendered.

Tested in Chrome, FF, and IE.

javiertoledo commented 10 years ago

The plugin was not designed to act via JS as a conventional input, but it would definitively be a very nice addition! I'll address this on some near future as soon as I find some spare time, thanks for your feedback!

Meanwhile you should be able to activate the stars and assign the value with a function similar to this one (I haven't tested it):

function assign_val(ratingInputSelector, value) {
  var selectedStar = $(ratingInputSelector).find('[data-value=' + value + ']');
  selectedStar.removeClass('glyphicon-star-empty').addClass('glyphicon-star');
  selectedStar.prevAll('[data-value]').removeClass('glyphicon-star-empty').addClass('glyphicon-star');
  selectedStar.nextAll('[data-value]').removeClass('glyphicon-star').addClass('glyphicon-star-empty');
  selectedStar.siblings('input').val(value).trigger('change');
}
wloescher commented 10 years ago

Works like a charm...cheers!

wloescher commented 10 years ago

I spoke slightly too soon...I needed to edit just the first line of the function to go up to parent (div.rating-input) of my ratingInputSelector to get it to work correctly. var selectedStar = $(ratingInputSelector).parent().find("[data-value=" + value + "]");

findchris commented 10 years ago

Thank you both. This worked just as I wanted it to, allowing the same input to be used for both new and existing records.
:+1: on merging this functionality!