I have a problem with my code. I'm new to jQuery. It's perfect, I could do almost everything I wanted, except for one thing I can not solve: I can not get it to uncheck if clicked again, getting like new.
The following is the code below:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Pontos</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<style>
.rating {
overflow: hidden;
display: inline-block;
position: absolute;
font-size:18px;
color: #000000;
}
.rating-star {
padding: 0 1px;
margin: 0;
cursor: pointer;
display: block;
float: right;
}
.rating-star:after {
position: relative;
font-family: FontAwesome;
content:'\f1db';
}
.rating-star.checked ~ .rating-star:after,
.rating-star.checked:after {
content:'\f111';
}
.rating:hover .rating-star:after {content:'\f1db';}
.rating-star:hover ~ .rating-star:after,
.rating-star:hover:after {
content:'\f111' !important;
}
/* Just for the demo */
body {
margin: 20px;
}
</style>
</head>
<body>
<div class="rating">
<span class="rating-star" data-value="5"></span>
<span class="rating-star" data-value="4"></span>
<span class="rating-star" data-value="3"></span>
<span class="rating-star" data-value="2"></span>
<span class="rating-star" data-value="1"></span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$('.rating-star').click(function() {
$(this).parents('.rating').find('.rating-star').removeClass('checked');
$(this).addClass('checked');
});
</script>
</body>
</html>