Reputation: 741
I have a very simple star rating system. The issue I am having is with an onclick event that is supposed to change the colour of the star when clicked.
I've created a jsFiddle for what I have so far.
HTML
<div class="rating">
<span id="five-stars" onclick="vote(5); return false;
document.getElementById('five-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 5 stars out of 5">
☆</span><span id="fout-stars" onclick="vote(4); return false;
document.getElementById('four-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 4 stars out of 5">
☆</span><span id="three-stars" onclick="vote(3); return false;
document.getElementById('three-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 3 stars out of 5">
☆</span><span id="two-stars" onclick="vote(2); return false;
document.getElementById('two-stars').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 2 stars out of 5">
☆</span><span id="one-star" onclick="vote(1); return false;
document.getElementById('one-star').style.color = '#ff6266'"
data-toggle="tooltip" data-placement="bottom" title="Rate 1 star out of 5">
☆</span>
</div>
CSS
.rating {
color:#02dbdd;
font: 700 26px/normal 'Montserrat', sans-serif;
text-transform:uppercase;
display:block;
cursor:pointer;
margin-right:100px;
}
.rating {
unicode-bidi: bidi-override;
direction: rtl;
}
.rating > span {
display: inline-block;
position: relative;
width: 1.1em;
}
.rating > span:hover:before,
.rating > span:hover ~ span:before {
content: "\2605";
position: absolute;
}
Not sure why it won't change?
(The RTL thing is a whole other problem to tackle... lol)
Upvotes: 0
Views: 1395
Reputation: 943979
You have three different problems that directly impact your test:
$.post
it errors because $
is undeclared. Add jQuery.return false
in the function before you try to set the colour. Naturally the function returns before it reaches that line. Remove the return false
.Upvotes: 3
Reputation: 9691
Fiddle: http://jsfiddle.net/nn5ytthL/5/
<div class="rating">
<span id="five-stars" onclick="vote(5); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 5 stars out of 5">☆</span>
<span id="fout-stars" onclick="vote(4); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 4 stars out of 5">☆</span>
<span id="three-stars" onclick="vote(3); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 3 stars out of 5">☆</span>
<span id="two-stars" onclick="vote(2); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 2 stars out of 5">☆</span>
<span id="one-star" onclick="vote(1); this.style.color = '#ff6266'" data-toggle="tooltip" data-placement="bottom" title="Rate 1 star out of 5">☆</span>
</div>
Upvotes: 2
Reputation: 1401
JS stop execution when can't find vote(5) function. Add it and remove return
from the middle of onclick
handler then it works.
function vote(){}
Here updated fiddle: http://jsfiddle.net/nn5ytthL/4/
Upvotes: 0