Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:17:53+00:00 2026-05-23T19:17:53+00:00

I am trying to write a star rating system in javascript that allows the

  • 0

I am trying to write a star rating system in javascript that allows the user to rerate (is that a word?). I am quite new to Javascript and programming in general, but here is what I have so far:

<div id="rateMe" title="Rate Me...">
     <a><img id="star1" onMouseOver="mOverRate(this);" onMouseOut="mOutRate(this);"
        onMouseDown="mClickRate(this);" src="star_empty.png"/></a>
     <a ><img id="star2" onMouseOver="mOverRate(this);" onMouseOut="mOutRate(this);" 
        onMouseDown="mClickRate(this);" src="star_empty.png"/></a>
     <a><img id="star3" onMouseOver="mOverRate(this);" onMouseOut="mOutRate(this);" 
        onMouseDown="mClickRate(this);" src="star_empty.png"/></a>
     <a><img id="star4" onMouseOver="mOverRate(this);" onMouseOut="mOutRate(this);" 
        onMouseDown="mClickRate(this);" src="star_empty.png"/></a>
     <a><img id="star5" onMouseOver="mOverRate(this);" onMouseOut="mOutRate(this);"
        onMouseDown="mClickRate(this);" src="star_empty.png"/></a>


     <label id="info0"></label>
     <label id="info1"></label>
     <label id="info2"></label>
     <label id="info3"></label> 
     <label id="info4"></label>
</div>



<script type="text/javascript">
        var Rated = false;

        // Declare an array that holds refers to the star img objects
        var aryImg = new Array(document.getElementById("star1"), document.getElementById("star2"), 
            document.getElementById("star3"), document.getElementById("star4"),
            document.getElementById("star5"));

        // Decare an array that will be used to store the star rating
        // when a user clicks and chooses a rating
        aryStoredImg = aryImg;




        function mOverRate(that)
        {

            var myImg = that;

            // Changes the star image to filled (and any images to the right
            // of it) if the mouse is hovering over it
            switch (myImg.id)
            {
                case "star1":
                    aryImg[0].src = "star_filled.png";
                    break;
                case "star2":
                    for (var i = 0; i <= 1; i++)
                    {
                        aryImg[i].src = "star_filled.png";
                    }
                    break;
                case "star3":
                    for (var i = 0; i <= 2; i++)
                    {
                        aryImg[i].src = "star_filled.png";
                    }
                    break;
                case "star4":
                    for (var i = 0; i <= 3; i++)
                    {
                        aryImg[i].src = "star_filled.png";
                    }
                    break;
                case "star5":
                    for (var i = 0; i <= 4; i++)
                    {
                        aryImg[i].src = "star_filled.png";
                    }
                    break;
            }

        }


        function mClickRate(that)
        {

            // This attempts to store the state of the imgs
            // after the user clicks a star
            for (var i = 0; i < aryImg.length; i++)
            {
                aryStoredImg[i].src = aryImg[i].src;
            }

            Rated = true;
        }




        function mOutRate(that)
        {
            var myImg = that;

            if (Rated)
            {
                // This replaces the images displayed with the
                // images that were stored at the time the user
                // clicked on a star
                for (var i = 0; i < aryImg.length; i++)
                {

                    aryImg[i].src = aryStoredImg[i].src;

                }
            }
            else
            {
                // This resets all of the images after the mouse
                // out event
                for (var i = 0; i < aryImg.length; i++)
                { 
                    aryImg[i].src = "star_empty.png";

                }
            }

        }

In the mClickRate() function I wanted to store the current source so that I could recall it in the mOutRate() function to restore the user’s choice if he does not click again. However, after a little research (okay a lot of research) I found out that my new array was pointing to the same reference.

I have tried using the array.splice and I have also tried using a loop without any luck. If anyone has any help to offer in copying this type of array by value instead of reference or advice on how to make my script better please let me know.

Oh, and did I said I was a beginner (Please keep that in mind when responding)? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:17:54+00:00Added an answer on May 23, 2026 at 7:17 pm

    I’m not sure I’m following exactly what you’re asking, but if what you want to do is to save the current user’s settings, then you should save the actual state (e.g. the number of stars), not image references used in the UI. So, when you want to save the state just look at how many stars are set and save that single number. If you want to restore that state sometime in the future, you just loop through and set the right number of images to match that number. That really ought to work much better than saving image references. Another advantage is that assigning numbers is always by copy so you don’t have to worry about references.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI all, I'm trying to write a simple star rating component. I'm fairly new
I'm trying write a Ruby script that checks if user credentials are valid using
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
im trying to write an app that will display a list off lines from
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a custom WPF ValidationRule to enforce that a certain property
Trying to write a code that searches hash values for specific string's (input by
I'm trying write a simple perl script that reads some fields from a password
Im trying to write out an image file that has the following structure: P5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.