Skip to content
Advertisement

How to set 4.5 stars with jQuery on page load?

I am trying to make sure that when the page is loaded, the rating is automatically filled with 4.5. There must be four and a half stars active (yellow) with jQuery.

My code:

JavaScript
JavaScript

What can I do in jQuery to give 4.5 stars to this?

Advertisement

Answer

The easiest method is to set the value directly in the HTML:

JavaScript

using the checked attribute.

If you need to use jquery, your radio (in the code provided in the question) has an id so you can select it using the id and use .prop, wrap in a doc ready to be safe:

JavaScript

If you don’t have an id, eg if you have multiple ratings on your page, then you can identify them using the value, eg:

JavaScript

(make sure the 4.5 is in quotes otherwise it gets an error)

Updated snippet with id:

JavaScript
JavaScript
JavaScript

Snippet without id

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement