Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp needed in 5 star rating - Jquery
    primarykey
    data
    text
    <p>I'm trying to build a star-rating system. The code that I have now does the following :</p> <p>Highlight stars when mouse hovers over it, but revert back to previous state when mouse is removed.</p> <p>I want to add the following functionality to it : When I click on a star ,the number of stars I clicked should remain highlighted even after removing the mouse from the stars. </p> <p>Can anyone please help me on this ?</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" &gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;title&gt;AJAX 5 Star Rating&lt;/title&gt; &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;script&gt; // This is the first thing we add ------------------------------------------ $(document).ready(function() { $('.rate_widget').each(function(i) { var widget = this; var out_data = { widget_id : $(widget).attr('id'), fetch: 1 }; $.post( 'ratings.php', out_data, function(INFO) { $(widget).data( 'fsr', INFO ); set_votes(widget); }, 'json' ); }); $('.ratings_stars').hover( // Handles the mouseover function() { $(this).prevAll().andSelf().addClass('ratings_over'); $(this).nextAll().removeClass('ratings_vote'); }, // Handles the mouseout function() { $(this).prevAll().andSelf().removeClass('ratings_over'); // can't use 'this' because it wont contain the updated data set_votes($(this).parent()); } ); // This actually records the vote $('.ratings_stars').bind('click', function() { var star = this; var widget = $(this).parent(); var clicked_data = { clicked_on : $(star).attr('class'), widget_id : $(star).parent().attr('id') }; $.post( 'ratings.php', clicked_data, function(INFO) { widget.data( 'fsr', INFO ); set_votes(widget); }, 'json' ); }); }); function set_votes(widget) { var avg = $(widget).data('fsr').whole_avg; var votes = $(widget).data('fsr').number_votes; var exact = $(widget).data('fsr').dec_avg; window.console &amp;&amp; console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes); $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote'); $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); $(widget).find('.total_votes').text( votes + ' votes recorded (' + exact + ' rating)' ); } // END FIRST THING &lt;/script&gt; &lt;style&gt; .rate_widget { border: 1px solid #CCC; overflow: visible; padding: 10px; position: relative; width: 180px; height: 32px; } .ratings_stars { background: url('star_empty.png') no-repeat; float: left; height: 28px; padding: 2px; width: 32px; } .ratings_vote { background: url('star_full.png') no-repeat; } .ratings_over { background: url('star_highlight.png') no-repeat; } .total_votes { background: #eaeaea; top: 58px; left: 0; padding: 5px; position: absolute; } .event_choice { font: 10px verdana, sans-serif; margin: 0 auto 40px auto; width: 180px; } h1 { text-align: center; width: 400px; margin: 20px auto; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt; Rate the event! &lt;/h1&gt; &lt;div class='event_choice'&gt; Rate: Raiders of the Lost Ark &lt;div id="r1" class="rate_widget"&gt; &lt;div class="star_1 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_2 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_3 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_4 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_5 ratings_stars"&gt;&lt;/div&gt; &lt;div class="total_votes"&gt;vote data&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class='event_choice'&gt; Rate: The Hunt for Red October &lt;div id="r2" class="rate_widget"&gt; &lt;div class="star_1 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_2 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_3 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_4 ratings_stars"&gt;&lt;/div&gt; &lt;div class="star_5 ratings_stars"&gt;&lt;/div&gt; &lt;div class="total_votes"&gt;vote data&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload