Reputation:
I want to build a 5 star rating system using jQuery, PHP and MySQL. I am following this tutorial to make it, but I am getting an error in the jQuery part. I am good at PHP and MySQL but have little knowledge about jQuery and Ajax.
jQuery code:
$(function(){
$('.star').mouseover(function (){
var star = $(this).index() 1;
$(this).parent().css("background-position","0" - (32 * star) "px");
});
$('.star-rating').mouseout(function (){
var originalresult = $(this).attr('id').split('result')[1];
$(this).css("background-position","0 -" (32 * originalresult) "px");
});
});
HTML for this:
<div class="star-rating" id="rating1result0">
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
<div class="star"></div>
</div>
<div class="result">
<span style="color:green">0</span> (0)
</div>
and the CSS is:
.star-rating {
width: 80px;
height: 16px;
float: left;
background: url(_ls-global/images/layout-images/urating.png);
cursor: pointer;
}
.star {
width:16px;
height:16px;
float:left;
}
.result {
float:left;
height:16px;
color:#454545;
margin-left:5px;
line-height:110%;
}
I tried changing the code like this but got an unexpected result.
$(function(){
$('.star').mouseover(function (){
var star = $(this).index()+1;
$(this).parent().css("background-position","32px");
});
$('.star-rating').mouseout(function (){
var originalresult = $(this).attr('id').split('result')[1];
$(this).css("background-position","32px");
});
});
Please can somebody suggest to me what is wrong in the code?
Upvotes: 1
Views: 381
Reputation: 174
var star = $(this).index() 1;
what is the digit 1 supposed to do there? That's supposed to be the parse error.
Upvotes: 0
Reputation:
Finally Got it.
$(function(){
$('.star').mouseover(function (){
var star = $(this).index()+1;
var x =(32 * star);
$(this).parent().css("backgroundPosition","0% " +(-x)+ "px");
});
$('.star-rating').mouseout(function (){
var originalresult = $(this).attr('id').split('result')[1];
var y =(32 * originalresult);
$(this).css("background-position","0%" +(-y)+ "px");
});
});
hope this helps to others having same issue.
Upvotes: 2
Reputation: 484
$(function(){
$('.star').mouseover(function (){
var star = $(this).index();
var newvalue =0-(32 * star);
$(this).parent().css("background-position",newvalue+" px");
});
$('.star-rating').mouseout(function (){
var originalresult = $(this).attr('id').split('result')[1];
var newvalue =0 -(32 * originalresult);
$(this).css("background-position",newvalue+"px");
});
});
Try this.
Upvotes: 1