SIGN UP MEMBER LOGIN:    
ARTICLE

Rate Control in JavaScript

Posted by Mahak Gupta Articles | ASP, JavaScript, CSS June 27, 2011
Here we create a Rate Control with the help of JavaScript.
Reader Level:
 


Here we create a Rate Control with the help of JavaScript.

RateCont1.gif

Step 1: First we take three Images (Star1, Star2, Star3) of the stars in the Image Folder.

RateCont2.gif

Step 2: After that we use the Image Control in our form and a label control (lblrate) to show the rating:

<img id="img1" alt=""  src="Image/Star1.png" width="20" />
<img id="img2" alt=""  src="Image/Star1.png" width="20" />
<img id="img3" alt=""  src="Image/Star1.png" width="20" />
<img id="img4" alt=""  src="Image/Star1.png" width="20" />
<img id="img5 alt=""  src="Image/Star1.png" width="20" />

<asp:Label ID="lblrate" runat="server"></asp:Label>

Step 3: Now we write the JavaScript functions Show() and Hide().

Show(): With this function we set the Image and Label like this:

RateCont3.gif

Hide(): With this function we set the Default Image and set the rating according to It.

<img id="img1" onmouseover="Show1()" onmouseout="Hide1()" alt="" src="Image/Star1.png" width="20" />
<img id="img2" onmouseover="Show2()" onmouseout="Hide2()" alt="" src="Image/Star1.png" width="20" />
<img id="img3" onmouseover="Show3()" onmouseout="Hide3()" alt="" src="Image/Star1.png" width="20" />
<img id="img4" onmouseover="Show4()" onmouseout="Hide4()" alt="" src="Image/Star1.png" width="20" />
<img id="img5" onmouseover="Show5()" alt="" src="Image/Star1.png" width="20" />

Note: We set Star2 as the Image when we put mouse over on it. And we set Star1 as the Image on the onmouseout event.

<script language="javascript" type="text/javascript">
        function Show1() {
            document.getElementById('img1').src = "Image/Star2.png";
            document.getElementById('lblrate').innerHTML = "1";
        }
        function Show2() {
            document.getElementById('img1').src = "Image/Star2.png";
            document.getElementById('img2').src = "Image/Star2.png";
            document.getElementById('lblrate').innerHTML = "2";
        }
        function Show3() {
            document.getElementById('img1').src = "Image/Star2.png";
            document.getElementById('img2').src = "Image/Star2.png";
            document.getElementById('img3').src = "Image/Star2.png";
            document.getElementById('lblrate').innerHTML = "3";
        }
        function Show4() {
            document.getElementById('img1').src = "Image/Star2.png";
            document.getElementById('img2').src = "Image/Star2.png";
            document.getElementById('img3').src = "Image/Star2.png";
            document.getElementById('img4').src = "Image/Star2.png";
            document.getElementById('lblrate').innerHTML = "4";
        }

        function Show5() {
            document.getElementById('img1').src = "Image/Star2.png";
            document.getElementById('img2').src = "Image/Star2.png";
            document.getElementById('img3').src = "Image/Star2.png";
            document.getElementById('img4').src = "Image/Star2.png";
            document.getElementById('img5').src = "Image/Star2.png";
            document.getElementById('lblrate').innerHTML = "5";
        }
        function Hide1() {
            if (document.getElementById('img1').src != "Image/Star3.png") {
                document.getElementById('lblrate').innerHTML = "";
            }

            if (document.getElementById('img2').src != "Image/Star1.png") {
                document.getElementById('img2').src = "Image/Star1.png";
                document.getElementById('img1').src = "Image/Star1.png";
            }

        }
        function Hide2() {
            if (document.getElementById('img2').src != "Image/Star3.png") {
                document.getElementById('lblrate').innerHTML = "2";
            }
            if (document.getElementById('img3').src != "Image/Star1.png") {
                document.getElementById('img3').src = "Image/Star1.png";

            }
        }
        function Hide3() {
            if (document.getElementById('img3').src != "Image/Star3.png") {
                document.getElementById('lblrate').innerHTML = "3";
            }

            if (document.getElementById('img4').src != "Image/Star1.png") {
                document.getElementById('img4').src = "Image/Star1.png";

            }
        }
        function Hide4() {
            if (document.getElementById('img4').src != "Image/Star3.png") {
                document.getElementById('lblrate').innerHTML = "4";
            }
            if (document.getElementById('img5').src != "Image/Star1.png") {
                document.getElementById('img5').src = "Image/Star1.png";
               
            }

        }

</script>

Note: Here we set the innerHTML property of the Label (lblrate). To display the rate according to the function.

Step 4: Now we write the onclick function in the JavaScript. It is useful when the user clicks on the star.

RateCont4.gif

   
<img id="img1" onmouseover="Show1()" onclick="ShowRate1()" onmouseout="Hide1()" alt="" src="Image/Star1.png" width="20" />
    <img id="img2" onmouseover="Show2()" onclick="ShowRate2()" onmouseout="Hide2()" alt="" src="Image/Star1.png" width="20" />
    <img id="img3" onmouseover="Show3()" onclick="ShowRate3()" onmouseout="Hide3()" alt="" src="Image/Star1.png" width="20" />
    <img id="img4" onmouseover="Show4()" onclick="ShowRate4()" onmouseout="Hide4()" alt="" src="Image/Star1.png" width="20" />
    <img id="img5" onmouseover="Show5()" onclick="ShowRate5()"  alt="" src="Image/Star1.png" width="20"
/>


Here we use the ShowRate() function. With the help of this function we set the Image.

(Star2 to Star3) and set the Label according to the value.

function ShowRate1() {
            document.getElementById('img1').src = "Image/Star3.png";
            document.getElementById('img2').src = "Image/Star1.png";
            document.getElementById('img3').src = "Image/Star1.png";
            document.getElementById('img4').src = "Image/Star1.png";
            document.getElementById('img5').src = "Image/Star1.png";
            document.getElementById('lblrate').innerHTML = "1";
        }
        function ShowRate2() {
            document.getElementById('img2').src = "Image/Star3.png";
            document.getElementById('img1').src = "Image/Star3.png";
            document.getElementById('img3').src = "Image/Star1.png";
            document.getElementById('img4').src = "Image/Star1.png";
            document.getElementById('img5').src = "Image/Star1.png";
            document.getElementById('lblrate').innerHTML = "2";
        }
        function ShowRate3() {
            document.getElementById('img3').src = "Image/Star3.png";
            document.getElementById('img1').src = "Image/Star3.png";
            document.getElementById('img2').src = "Image/Star3.png";
            document.getElementById('img4').src = "Image/Star1.png";
            document.getElementById('img5').src = "Image/Star1.png";
            document.getElementById('lblrate').innerHTML = "3";
        }
        function ShowRate4() {
            Show4();
            document.getElementById('img4').src = "Image/Star3.png";
            document.getElementById('img2').src = "Image/Star3.png";
            document.getElementById('img3').src = "Image/Star3.png";
            document.getElementById('img1').src = "Image/Star3.png";
            document.getElementById('img5').src = "Image/Star1.png";
            document.getElementById('lblrate').innerHTML = "4";
        }

function ShowRate5() {

            Show5();
            document.getElementById('img5').src = "Image/Star3.png";
            document.getElementById('img2').src = "Image/Star3.png";
            document.getElementById('img3').src = "Image/Star3.png";
            document.getElementById('img4').src = "Image/Star3.png";
            document.getElementById('img1').src = "Image/Star3.png";
            document.getElementById('lblrate').innerHTML = "5";
           
        }

 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks for sharing...

Posted by Dinesh Beniwal Jun 29, 2011

When I am clicking or onmouseover on imag1,it displays the color as well as label but when onmouseout it didn't display color or label.

Posted by Mahesh Babu Jun 28, 2011

Nice and cool one

Posted by Dorababu M Jun 28, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor