id attribute used for avg value
-
Hi there,
while adding some ajax trigger for the plugin i noticed that the id attribute is used to set and get the values which can be the same for more than one element on the page because its just a number without any prefix and if you add the rating to an archive page more posts will have the same element with same id.
while i worked on a nother topic i started to reimplement the shortcode function and now i started changing the html output in it with
<span class="wpcr_averageStars" id="'.$result.'">
replaced by
<span class="wpcr_averageStars" data-wpcravg="'.$result.'">
then based on another topic i adoped my themes javascript like so:
;(function($){ 'use strict'; var min_stars = 0, max_stars = 5, data_name = 'wpcravg', events = 'ready ajaxComplete', selectors = { averageStars: '.wpcr_averageStars' }, set_stars = function(){ $(selectors.averageStars).each(function(){ // $this uses the sizzle engine only once // $(this).attr('id'), id must be unique in document: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id // parseInt to get a number out of it // add span directly to .html() also saves one sizzle call var $this = $(this), avg = parseInt($this.data(data_name)), stars_width = Math.max(min_stars, (Math.min(max_stars, avg))) * 16; $this.html('<span style="width:' + stars_width + 'px;"></span>'); }); }; $(document).on(events, set_stars); })(jQuery);
hope it helps in further development.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘id attribute used for avg value’ is closed to new replies.