I want a star rating system please check the JSFiddle .I want to stick the rating on mouse click.
HTML:
<div class="rating-star rating-star-off" data-value="0"></div>
<div class="rating-star rating-star-off" data-value="1"></div>
<div class="rating-star rating-star-off" data-value="2"></div>
<div class="rating-star rating-star-off" data-value="3"></div>
<div class="rating-star rating-star-off" data-value="4"></div>
</div>
CSS:
.rating-star{width:25px;height:25px;float:left;margin-right:5px;}
.rating-star-off{background:#eee url(star-off.svg) no-repeat;}
.rating-star-on{background:#000 url(star-on.svg) no-repeat;}
jQuery:
$(function(){
$(".rating-star").click(function(e){
$(this).prevAll().andSelf().addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
});
$(".rating-star").hover(function(){
$(this).prevAll().andSelf().addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
});
$(".rating-star").mouseout(function(){
$(".rating-star").removeClass('rating-star-on');
});
});
http://jsfiddle.net/piyush_dezi/a67gv2o7/
Thanks in advance for your help.
One way is to add another class for selected
to your css rules
.rating-star-on,
.rating-star-selected
{background:#000 url(star-on.svg) no-repeat;}
And change the click handler to add this selected class.
$(".rating-star").click(function(e){
$(this).prevAll().andSelf().addClass('rating-star-selected');
$(this).nextAll().removeClass('rating-star-on');
});
You will likely need to make some adjustments if you want user to be able to change their rating also
Add this line to CSS:
.rating-star-hover {background:#000 url(star-on.svg) no-repeat;}
Update JS:
$(this).prevAll().andSelf().removeClass('rating-star-off').addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
});
$(".rating-star").hover(function(){
$(this).prevAll().andSelf().addClass('rating-star-hover');
$(this).nextAll().removeClass('rating-star-on');
});
$(".rating-star").mouseout(function(){
$(".rating-star").removeClass('rating-star-hover');
});
The reason why it's not getting executed is that when we click on the div, the rating-on class is added but as we mouseout the class is been removed. So the rating class is not changed. You can also do this using an boolean variable. Check this :
`http://jsfiddle.net/bandhavya/we3mx6vt/`
First your click event add classes to highlight the stars after it mouse out event remove these classes. In mouse click event unbind mouse out event so it will not remove classes from your stars. Also instead of using anonymous mouse out function use name function so your can bind it again for other starts.
$(function(){
$(".rating-star").mouseout(mouseOutFunction);
function mouseOutFunction(){
$(".rating-star").removeClass('rating-star-on');
}
$(".rating-star").click(function(e){
$(this).prevAll().andSelf().addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
$(".rating-star").bind( "mouseout", mouseOutFunction);
$(this).unbind( "mouseout" );
});
$(".rating-star").mouseenter(function(){
$(this).prevAll().andSelf().addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
});
});
Here is my Parent component's render function: render() { const users = [ 'tom': { phone: '123', email: 'hotmail' }, 'rob': { phone: '321', ...
Here is my Parent component's render function: render() { const users = [ 'tom': { phone: '123', email: 'hotmail' }, 'rob': { phone: '321', ...
I have following parent route in my app: .state('app',{ url: '/app', templateUrl: 'views/app.html', resolve: loadSequence('modernizr','moment'), ...
I have following parent route in my app: .state('app',{ url: '/app', templateUrl: 'views/app.html', resolve: loadSequence('modernizr','moment'), ...
Im trying to use http://www.chartjs.org/ to create a pie chart while retrieve database values. Following are my codes callactiveinactive.php <?php require connectDB.php; $sql = "SELECT ...
Im trying to use http://www.chartjs.org/ to create a pie chart while retrieve database values. Following are my codes callactiveinactive.php <?php require connectDB.php; $sql = "SELECT ...
i have tried to perform axios.delete operation in react, unfortunately it is not working, please help me to solve this issue, export function DeletePatient(token,deletePatient) { return axios....
i have tried to perform axios.delete operation in react, unfortunately it is not working, please help me to solve this issue, export function DeletePatient(token,deletePatient) { return axios....