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

  • Home
  • SEARCH
  • 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 1069331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:24:32+00:00 2026-05-16T20:24:32+00:00

The script I have been working on for a star rating system, saves the

  • 0

The script I have been working on for a star rating system, saves the filled in stars with an onClick event and changes the onmouseover & onmouseout values to null so moving the mouse off them afterwards won’t mess that up, and the form has multiple ratings and a clear button at the bottom, which i need to have reset the onmouseover & onmouseout events, through the function below, but within those, ratings[y] and x are taken to be literal rather than what they contain, and makes the onmouse events fail because the parameters are incorrect. How do I put in the variables when changing the events this way?

     var ratings = new Array("happy", "clean");
  for(y = 0; y < 2; y++)
  {
   for(x = 0; x < 5; x++)
   {
    fullname =  ratings[y] + '_' + x;
    document.getElementById(fullname).onmouseover = function onmouseover() { star_rate(ratings[y], x, 1); };
    document.getElementById(fullname).onmouseout = function onmouseover() { star_rate(ratings[y], x, 2); };
   }
  }
  • 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-16T20:24:33+00:00Added an answer on May 16, 2026 at 8:24 pm

    You can do it by using a function call to create a next context for each handler (but see below), like this:

    var ratings = new Array("happy", "clean");
    for(y = 0; y < 2; y++)
    {
        for(x = 0; x < 5; x++)
        {
            fullname =  ratings[y] + '_' + x;
            (function(thisx, thisy) {
                document.getElementById(fullname).onmouseover = function() {
                    star_rate(ratings[thisy], thisx, 1);
                };
                document.getElementById(fullname).onmouseout = function() {
                    star_rate(ratings[thisy], thisx, 2);
                };
            })(x, y);
        }
    }
    

    That works by passing x and y as parameters into a function, which then sets up the event handlers using the arguments rather than the outer loop’s version of x and y. (I’ve also removed the function names in the event handler assignments above. Using a name in a function expression [as opposed to a function declaration] is problematic in some browsers, more here. Also, you’d called them both onmouseover, which probably isn’t what you meant to do. 🙂 )

    But that’s not how I’d recommend doing it. It creates a new function for each element. Instead, I’d probably store the necessary information on the element itself as an attribute, and use a common function for all of this. That’s what HTML5’s data- prefix is for (and it works now, today, even though technically not valid). Something vaguely like this:

    var ratings = new Array("happy", "clean");
    var element;
    for(y = 0; y < 2; y++)
    {
        for(x = 0; x < 5; x++)
        {
            fullname =  ratings[y] + '_' + x;
            element = document.getElementById(fullname);
            element.setAttribute('data-star-x', x);
            element.setAttribute('data-star-y', y);
            element.onmouseover = handleMouseOver;
            element.onmouseout = handleMouseOut;
        }
    }
    
    function handleMouseOver() {
        star_rate(this.getAttribute('data-star-y'), this.getAttribute('data-star-x'), 1);
    }
    function handleMouseOut() {
        star_rate(this.getAttribute('data-star-y'), this.getAttribute('data-star-x'), 2);
    }
    

    You could even use event bubbling (since mouseover and mouseout bubble) to hook the events on the overall container rather than each element, since you’re getting the data from the element. (That’s sometimes called “event delegation”.)

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

Sidebar

Related Questions

I have been working on this for 1 hour with no such luck. Any
I have been working with jquery-ui's sortable demo for a while. On looking closely,
I have been trying to execute the following UNIX shell script which is not
I've been working on this site - I used the phpdug script to help
Hey folks, I have to apologize for my inexperience here, I've been working on
today I've been working on loading dynamic javascript code (files). The solution I use
I am working on a God script to monitor my Unicorns. I started with
I'm trying to write a jQuery script (I have never written one before and
I've set up a simple, single-service server which has been working just fine with

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.