JQuery along with Font Awesome: Setting the default value for star rating

The Situation:

I am currently working on a system where users can rate various products using Laravel. Each product has an average rating which is then used to display stars. These ratings are shown on the "Product Listing" page, which contains multiple products.

My Objective:

I am looking to pass a variable through a div in order to populate the stars when the page loads. I have attempted the following code:

<span class="rating" rating="3">
  <span class="star "></span>
  <span class="star "></span>
  <span class="star"></span>
  <span class="star"></span>
  <span class="star"></span>
</span>

Here is the jQuery script I have tried:

$('.rating').each(function (event) {

  var rating = $(this).attr('rating');

  for(var i = 0; (i < rating); i++)
  {
        $('.rating span.star').eq(i).addClass('filled');
  }

});

However, I am facing issues with the stars being populated incorrectly and only taking into account the last product's rating while ignoring the rest.

You can see a live example here:

http://jsfiddle.net/qes0tgrt/

Answer №1

To create stars in the correct direction, you will need to modify the .rating class as follows:

.rating {
       unicode-bidi:bidi-override;
       direction:ltr;
       font-size:30px;
}

To include half star functionality, add these classes:

span.star.half-filled:before{
    content: "\f089";
    color:#e3cf7a;
}
span.star.half-filled:after{
    content: "\f006";
    color:#e3cf7a;
    margin-left:-20px;
}

The JavaScript script should look like this:

$('.rating').each(function (event) {
   var rating = $(this).attr('rating');
   for(var i = 0; (i < rating); i++)
   {
      $(this).find('span.star').eq(i).addClass('filled');
   }
   //if rating has decimal places means its a half star
   if(rating % 1 > 0)
      $(this).find('span.star').eq(i - 1).addClass('half-filled');
});

For half star usage, the rating attribute should be utilized like so:

<span class="rating" rating="2.5">

Here's an updated working example : fiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Utilize an AJAX call to dynamically retrieve and add results for every PHP loop

I currently have a functional AJAX request that calls a PHP file with some looping actions. Depending on certain parameters, the request can take several minutes to complete, which is not ideal for user experience. How can I modify my code so that results ...

When utilizing the 'checkAll' method in a Bootstrap table, it will only select the checkboxes displayed on the current page

I currently have a Bootstrap table on my HTML5 page. When I use the code $('#bk-table').bootstrapTable('checkAll'), only the checkboxes on the current page are checked. Is there a way to check all the checkboxes on all pages? Here i ...

Difficulty with Script Causing Margin Bottom to Fail

Check out my website at this link. Below is a div I have created: <div class="home" style="background-image: url(http://payload51.cargocollective.com/1/7/237315/3336908/HomeImage_o.jpg); background-attachment: fixed; height: 660px; width: 100%; opacit ...

Tips on aligning text vertically within a Bootstrap table

My goal is to vertically align the text in the center of its row within a bootstrap table, regardless of its length. I've been trying to achieve this using vertical-align: middle; line-height: 90px;. Adjusting the line-height seems to work for center ...

Utilizing AJAX to invoke the onget method with parameters in .NET Core

I attempted to invoke the onget method in .net core using an ajax call, but the method is being called without any parameters public void OnGet(string id,string refundReason ,int amount) { } I tried two different aj ...

angular literal containing a strike tag

I need help with inserting a string into a button using Angular literal, along with striking out the string based on a condition. button {{(players > 2) ? 'Start' : '<strike>Cannot start</strike>'}} The desired appearan ...

jQuery UI has encountered an issue: it seems that #accordion is not a recognized function

While attempting to integrate jQuery UI Accordion into a webpage, I encountered an error message in Firebug upon loading the page: #accordion is not a function The jQuery code I am using is as follows: <script type="text/javascript"> $(document).r ...

What are the techniques for implementing an if statement in CSS or resolving it through JavaScript?

Demo available at the bottom of the page Within my code, there is a div called #myDiv that defaults to an opacity of 0. Upon hovering over #myDiv, the opacity changes to 1. See the CSS snippet below: #myDiv { opacity: 0; } #myDiv:hover { opacity: 1 ...

Issues encountered while utilizing the jQuery function $.ajax()

I'm encountering issues with utilizing ajax in conjunction with jQuery... Here is the script I am using: $(document).ready(function() { $('rel[close]').click(function() { $(this.parent).close(); }); $('a[rel=trac ...

Building a Responsive Grid Layout with HTML and CSS: Layering one div on top of another

I'm struggling to find a solution to my problem. I attempted to set my div as absolute but it messed up the entire flexible grid layout. All I need is to place the div above the background div. Thank you in advance! CSS #mannequin { box-sizing: ...

jQuery asynchronous treeview collapse issue

I have implemented an async jQuery treeview to create a dynamic tree structure. The tree is populated based on the selection from a dropdown list. Initially, everything works perfectly when the treeview is loaded for the first time. However, upon changing ...

What is the method for loading a subcategory based on the category by invoking a jQuery function within the <td> element of a JavaScript function that adds rows dynamically?

Whenever I click the add row button, the category dropdown list successfully loads. However, when I select an option from this category list, the subcategory does not load any list. The Javascript function responsible for adding rows dynamically is as fol ...

Is it possible to close a JavaScript IFRAME using code?

Is there a way to close iframes using self.close()? It seems to not work since they are not considered popups, but is there another solution that can be used? ...

Issue with displaying spinner in bootstrap version 4.0.0

I am having trouble implementing a spinner on my website. I have followed the code below but still can't see the spinner showing up. Can someone please help me troubleshoot this? <!-- Bootstrap CSS --> <link rel="stylesheet" href= ...

Ways to confirm the closure of the reveal modal in Zurb Foundation 4

In the third version of Foundation, closing a reveal modal was as simple as triggering $('#myModal').trigger('reveal:close'); If the modal was open, it closed. If it was closed, it stayed that way. However, in Foundation 4, things ar ...

Is there a way to remove the bold styling from text next to JavaScript?

I recently launched a website at www.mvscaccounting.com, and I added a search engine made from javascript at the bottom of the page. Next to it, I wanted to put a "all rights reserved" notice. However, whenever I try to add any text next to the search engi ...

Creating a Product Box design with CSS and incorporating visual elements

I have created a simple box design in Photoshop that I want to use to display product information. The box consists of a Header, Body, and Button, each as separate images. How can I assemble these elements using CSS/HTML? I only need the header text at th ...

Dynamic table not triggering jQuery delegate event

Here is the code snippet from my .js file: //This script highlights rows in a results table $(document).ready(function () { $("table").delegate('td', 'mouseover mouseleave', function (e) { if (e.type == 'mouseover&apos ...

Is Firefox triggering setTimeout function prematurely (or is Date.getTime() inaccurate)?

Encountered an unusual issue with setTimeout() in Firefox 12. It doesn't always seem to wait the correct amount of time. Could it be a problem with the date's milliseconds? Take a look at this fiddle. Setting a setTimeout for 100ms seems to exec ...

What are the steps for adding information to a MySQL database with GWT, HTML, and either JDBC or Hibernate?

I have been working with GWT (Google Web Toolkit) version 2.5.1 and successfully developed a sample application to display the username and password. However, I am now facing challenges in inserting these values into a MySQL database table using JDBC or Hi ...