Jquery headaches today.I am now trying to combine hover and click events in my jquery, the latest edit to my hover function has cancelled out my click event, I can see why (i think). The remove class is overriding my click event.
What i want to achieve is that when i hover over a star all siblings are highlighted, basically follow the mouse, then when i have decided upon a star to choose i should be able to click it and all its siblings plus the one i clicked have the addClass method applied
What i have so far
/* Hover Function */
$(document).ready(function(){
$('.ratings li').hover(function(){
$(this).prevAll().andSelf().addClass('goldstar');
}, function() {
$(this).prevAll().andSelf().removeClass('goldstar');
});
});
/* Click Function */
$(document).ready(function(){
$('.ratings li').click(function(){
$(this).prevAll().andSelf().addClass('goldstar');
});
});
/* Click functions to add a value */
$(document).ready(function(){
$('#firstStar').click(function(){
$('#hiddenRating').val(1);
});
});
$(document).ready(function(){
$('#secondStar').click(function(){
$('#hiddenRating').val(2);
});
});
$(document).ready(function(){
$('#thirdStar').click(function(){
$('#hiddenRating').val(3);
});
});
$(document).ready(function(){
$('#fourthStar').click(function(){
$('#hiddenRating').val(4);
});
});
$(document).ready(function(){
$('#fifthStar').click(function(){
$('#hiddenRating').val(5);
});
});
The Form
<%= form_for @rating do |f| %>
<%= f.hidden_field :ratings, :id => "hiddenRating" %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :recipe_id, :value => @recipe.id %>
<div class="ratings">
<ul>
<li id="firstStar"></li>
<li id="secondStar"></li>
<li id="thirdStar"></li>
<li id="fourthStar"></li>
<li id="fifthStar"></li>
</ul>
</div>
Now I know there must be a better way of doing this, i have a lot of repeated code, but I am learning and would like to know of some ways to re factor this. is there a way to combine the .val methods into one?
With regards to the hover and click function I understand you can use the on method and pass hover and click? though not sure how to implement this
Any help appreciated,
Edit
Thanks