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 8781567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:20:11+00:00 2026-06-13T20:20:11+00:00

I wrote this script for a 5 star ratings system I made and it

  • 0

I wrote this script for a 5 star ratings system I made and it makes the stars light up as the user hovers over them, for example: if a user is hovered over the third star, that one shows the lit star image as well as the first two. The script works in all newer browsers except FireFox. I have racked my brain over this for a while and cannot figure out why its not working in firefox. Any help is appreciated. Also, I am only interested in responses about raw Javascript, not JQuery or any other framework. Thanks a bunch.

function btnSwap(){
var myBtns = document.getElementsByClassName('btns');
for(i=0; i<myBtns.length; i++){
    var elem = document.getElementById(myBtns[i].id);
    elem.src = "images/"+"starNorm.png";
    elem.onmouseover = btnOver;
    elem.onmouseout = btnOut;
    function btnOver(){
        var lit = document.getElementById(this.id);
        if(lit.id == "rb1"){
        lit.src = "images/"+"starOver.png";
        }
        if(lit.id == "rb2"){
        lit.src = "images/"+"starOver.png";
        document.getElementById('rb1').src = "images/"+"starOver.png";
        }
        if(lit.id == "rb3"){
        lit.src = "images/"+"starOver.png";
        document.getElementById('rb1').src = "images/"+"starOver.png";
        document.getElementById('rb2').src = "images/"+"starOver.png";
        }
        if(lit.id == "rb4"){
        lit.src = "images/"+"starOver.png";
        document.getElementById('rb1').src = "images/"+"starOver.png";
        document.getElementById('rb2').src = "images/"+"starOver.png";
        document.getElementById('rb3').src = "images/"+"starOver.png";
        }
        if(lit.id == "rb5"){
        lit.src = "images/"+"starOver.png";
        document.getElementById('rb1').src = "images/"+"starOver.png";
        document.getElementById('rb2').src = "images/"+"starOver.png";
        document.getElementById('rb3').src = "images/"+"starOver.png";
        document.getElementById('rb4').src = "images/"+"starOver.png";
        }

}

function btnOut(){
        var lit = document.getElementById(this.id);
        if(lit.id == "rb1"){
        lit.src = "images/"+"starNorm.png";
        }
        if(lit.id == "rb2"){
        lit.src = "images/"+"starNorm.png";
        document.getElementById('rb1').src = "images/"+"starNorm.png";
        }
        if(lit.id == "rb3"){
        lit.src = "images/"+"starNorm.png";
        document.getElementById('rb1').src = "images/"+"starNorm.png";
        document.getElementById('rb2').src = "images/"+"starNorm.png";
        }
        if(lit.id == "rb4"){
        lit.src = "images/"+"starNorm.png";
        document.getElementById('rb1').src = "images/"+"starNorm.png";
        document.getElementById('rb2').src = "images/"+"starNorm.png";
        document.getElementById('rb3').src = "images/"+"starNorm.png";
        }
        if(lit.id == "rb5"){
        lit.src = "images/"+"starNorm.png";
        document.getElementById('rb1').src = "images/"+"starNorm.png";
        document.getElementById('rb2').src = "images/"+"starNorm.png";
        document.getElementById('rb3').src = "images/"+"starNorm.png";
        document.getElementById('rb4').src = "images/"+"starNorm.png";
        }


    }
}

}

  • 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-06-13T20:20:13+00:00Added an answer on June 13, 2026 at 8:20 pm

    Don’t put the function declarations inside the for loop.

    function btnSwap(){
    
        var myBtns = document.getElementsByClassName('btns');
    
        // begin loop
        for(i=0; i<myBtns.length; i++){
            var elem = document.getElementById(myBtns[i].id);
            elem.src = "images/"+"starNorm.png";
            elem.onmouseover = btnOver;
            elem.onmouseout = btnOut;
        }
    
        // outside the loop.. now function declarations.
    
        function btnOver(){
            var lit = document.getElementById(this.id);
            if(lit.id == "rb1"){
            lit.src = "images/"+"starOver.png";
            }
            if(lit.id == "rb2"){
            lit.src = "images/"+"starOver.png";
            document.getElementById('rb1').src = "images/"+"starOver.png";
            }
            if(lit.id == "rb3"){
            lit.src = "images/"+"starOver.png";
            document.getElementById('rb1').src = "images/"+"starOver.png";
            document.getElementById('rb2').src = "images/"+"starOver.png";
            }
            if(lit.id == "rb4"){
            lit.src = "images/"+"starOver.png";
            document.getElementById('rb1').src = "images/"+"starOver.png";
            document.getElementById('rb2').src = "images/"+"starOver.png";
            document.getElementById('rb3').src = "images/"+"starOver.png";
            }
            if(lit.id == "rb5"){
            lit.src = "images/"+"starOver.png";
            document.getElementById('rb1').src = "images/"+"starOver.png";
            document.getElementById('rb2').src = "images/"+"starOver.png";
            document.getElementById('rb3').src = "images/"+"starOver.png";
            document.getElementById('rb4').src = "images/"+"starOver.png";
            }
    
        }
    
        function btnOut(){
            var lit = document.getElementById(this.id);
            if(lit.id == "rb1"){
            lit.src = "images/"+"starNorm.png";
            }
            if(lit.id == "rb2"){
            lit.src = "images/"+"starNorm.png";
            document.getElementById('rb1').src = "images/"+"starNorm.png";
            }
            if(lit.id == "rb3"){
            lit.src = "images/"+"starNorm.png";
            document.getElementById('rb1').src = "images/"+"starNorm.png";
            document.getElementById('rb2').src = "images/"+"starNorm.png";
            }
            if(lit.id == "rb4"){
            lit.src = "images/"+"starNorm.png";
            document.getElementById('rb1').src = "images/"+"starNorm.png";
            document.getElementById('rb2').src = "images/"+"starNorm.png";
            document.getElementById('rb3').src = "images/"+"starNorm.png";
            }
            if(lit.id == "rb5"){
            lit.src = "images/"+"starNorm.png";
            document.getElementById('rb1').src = "images/"+"starNorm.png";
            document.getElementById('rb2').src = "images/"+"starNorm.png";
            document.getElementById('rb3').src = "images/"+"starNorm.png";
            document.getElementById('rb4').src = "images/"+"starNorm.png";
            }
    
        }
    }
    

    Explanation

    Firefox respects conditional code blocks which wrap function declarations. For example:

    if (true) {
        function a() { return 1; }
    } else {
        function a() { return 2; }
    }
    a(); // => Chrome: 2, Firefox: 1
    

    Since a for code block might not be executed, Firefox tries to respect the conditional nature of the for loop. You can see this more directly in a while (false) loop.

    while(false) {
        function a() { return 'a'; }
    }
    a(); // => Chrome: 'a', Firefox: Throws an error.
    

    In Firefox, a is never defined because the condition never succeeds.

    However, if there is a code block which will always be executed no matter the condition, the function definition will be hoisted. We can see this in a do...while loop.

    var x = a;
    do {
        function a() { return 'a'; }
    } while(false);
    x(); // => Both browsers: 'a'
    

    No problem hoisting here because a do...while code block is always guaranteed to execute.


    Also, is there a reason you’re doing stuff like this?

    var elem = document.getElementById(myBtns[i].id)
    

    There’s no need to get an element by id if you already have it. Just use

    var elem = myBtns[i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone very helpful over on superuser wrote this script for me, the purpose of
I wrote like this Perl script.( /tmp/file is fuge) system(ssh host1 'rsync -av /tmp/file
I wrote this script which counts occurrences of particular pattern in a given file.
I wrote this script to for a contact form on my website, everything works
$(function(){ $('a').each(function(){ var x=this.href; this.href=www.somesitename.com/filter+this.href; }); }); i wrote the above jQuery script to
I wrote this script to first install the msi and then copy my application
I wrote this script to my live search: $(document).ready(function(){ $('input#search').keypress(function(){ $('ul#pacientes_hint').html(); var texto =
I wrote this script to interact with nessus and tell me what reports were
I wrote this sh script here. What it suppose to be doing is it
I wrote this python script to import a specific xls file into mysql. It

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.