How to make simple star rating? - javascript

I make my own star rating with html css, but it is not working true. If i use direction: rtl or float: right it works but i can't use rtl or float:right because of php.
How can i make it with pure css and html?
$('.stars a').on('click', function(){
$('.stars a').removeClass('active');
$(this).addClass('active');
});
.stars input {
position: absolute;
left: -999999px;
}
.stars a {
display: inline-block;
font-size: 0;
text-decoration: none;
}
.stars a:before {
position: relative;
font-size: 18px;
font-family: 'FontAwesome', serif;
display: block;
content: "\f005";
color: #9e9e9e;
}
.stars a:hover:before,
.stars a:hover ~ a:before,
.stars a.active:before,
.stars a.active ~ a:before {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<p class="stars">
<span>
<a class="star-1" href="#">1</a>
<a class="star-2" href="#">2</a>
<a class="star-3" href="#">3</a>
<a class="star-4" href="#">4</a>
<a class="star-5" href="#">5</a>
</span>
</p>

Starting to your HTML, I tried to reverse the color's change 'cause in pure CSS + & ~ works only on next elements. Then why you don't make the next a gray and not blue?
I tried to play only with your CSS, but I had to add also a class to span to work with active situation. Sorry :P
This is the result:
$('.stars a').on('click', function(){
$('.stars span, .stars a').removeClass('active');
$(this).addClass('active');
$('.stars span').addClass('active');
});
.stars input {
position: absolute;
left: -999999px;
}
.stars a {
display: inline-block;
padding-right:4px;
text-decoration: none;
margin:0;
}
.stars a:after {
position: relative;
font-size: 18px;
font-family: 'FontAwesome', serif;
display: block;
content: "\f005";
color: #9e9e9e;
}
span {
font-size: 0; /* trick to remove inline-element's margin */
}
.stars a:hover ~ a:after{
color: #9e9e9e !important;
}
span.active a.active ~ a:after{
color: #9e9e9e;
}
span:hover a:after{
color:blue !important;
}
span.active a:after,
.stars a.active:after{
color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<p class="stars">
<span>
<a class="star-1" href="#">1</a>
<a class="star-2" href="#">2</a>
<a class="star-3" href="#">3</a>
<a class="star-4" href="#">4</a>
<a class="star-5" href="#">5</a>
</span>
</p>
Hope it helps! :)

This uses radio buttons to achieve the effect
<h1>Pure CSS Star Rating Widget</h1>
<fieldset class="rating">
<input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>
https://codepen.io/jamesbarnett/pen/vlpkh

Simple star rating with pure javascript.
HTML
<div id="rating_bar"> </div>
CSS
#rating_bar {
width:100px;
height:100px;
display:inline-block;
display:inline;
}
Javascript
var content ='';
for(var i=1;i<=5;i++)
content += ' <span id="rate_'+i+'">✰</span> ';
document.getElementById('rating_bar').innerHTML=content;
var spans = document.getElementById("rating_bar")
.getElementsByTagName('span');
for(i=0;i<spans.length;i++)
spans[i].onmouseover=hoverStar;
function hoverStar()
{
for(i=0;i<this.id.charAt(5);i++)
spans[i].innerText='⭐';
for(i=this.id.charAt(5);i<5;i++)
spans[i].innerText='✰';
}
Sample Output:

HTML
<div class="rating" tabindex="1" onblur="calculateRating(this)">
<i class="fa fa-star-o" aria-hidden="true" value="1" onclick="clickStar(this)"></i>
<i class="fa fa-star-o" aria-hidden="true" value="2" onclick="clickStar(this)"></i>
<i class="fa fa-star-o" aria-hidden="true" value="3" onclick="clickStar(this)"></i>
<i class="fa fa-star-o" aria-hidden="true" value="4" onclick="clickStar(this)"></i>
<i class="fa fa-star-o" aria-hidden="true" value="5" onclick="clickStar(this)"></i>
</div>
<div class="rating-display"></div>
CSS
.clicked{
color:yellow;}
JAVASCRIPT
var rating = document.querySelector(".rating");
var ratingDisplayEle = document.querySelector(".rating-display");
//add event listener
function clickStar(ele){
var clickedStar = ele;
//value of the star
var ratingValue = parseInt(clickedStar.getAttribute("value"));
//change the color of the star
for(var i=0; i<ratingValue; i++){
rating.children[i].classList.add("clicked");
for(var j=ratingValue; j<=4; j++){
if(rating.children[j].classList.contains("clicked")){
rating.children[j].classList.remove("clicked");
}
}
}
}
//function to calculate rating
function calculateRating(ele){
//check how many elements are having clicked class
var ratingCount = 0;
for(var i=0; i<ele.children.length; i++){
if(ele.children[i].classList.contains("clicked")){
ratingCount++;
}
}
ratingDisplayEle.textContent = `You have selected ${ratingCount} rating out of 5`;
}

Simple star rating
function myFunction() {
var five = document.getElementById('1-star').checked;
document.getElementById("one").innerHTML = five;
}
/* component */
.star-rating {
display:flex;
flex-direction: row-reverse;
font-size:3em;
justify-content:space-around;
padding:0 .2em;
text-align:center;
width:5em;
}
.star-rating input {
display:none;
}
.star-rating label {
color:#ccc;
cursor:pointer;
}
.star-rating :checked ~ label {
color:#f90;
}
.star-rating label:hover,
.star-rating label:hover ~ label {
color:#fc0;
}
<div class="star-rating">
<input type="radio" id="5-stars" name="rating" value="5" />
<label for="5-stars" class="star">★</label>
<input type="radio" id="4-stars" name="rating" value="4" />
<label for="4-stars" class="star">★</label>
<input type="radio" id="3-stars" name="rating" value="3" />
<label for="3-stars" class="star">★</label>
<input type="radio" id="2-stars" name="rating" value="2" />
<label for="2-stars" class="star">★</label>
<input type="radio" id="1-star" name="rating" value="1" />
<label for="1-star" class="star">★</label>
</div>
<p id="one" onclick="myFunction()">Click me to change my HTML content (innerHTML).</p>

Related

Keep scroll position in an element on page refresh

I set a ul of product variants with horizontal scroll.
When i scroll and click on a variant the page refresh and the scroll return to start.
I need to maintain the posistion where it was first of refresh.
How can i do?
ul#group_17 {
width: 150px;
display: flex;
overflow: auto;
list-style: none;
}
li {margin: 0 10px 0 0}
.input-color {
position: absolute;
opacity: 0;
cursor: pointer;
height: 60px;
width: 60px;
}
span {
height: 60px;
width: 60px;
display: block;
}
<ul id="group_17">
<li class="float-xs-left input-container">
<label>
<input class="input-color" type="radio" data-product-attribute="17" name="group[17]" value="87">
<span class="color texture" style="background:black"></span>
</label>
</li>
<li class="float-xs-left input-container">
<label>
<input class="input-color" type="radio" data-product-attribute="17" name="group[17]" value="88" checked="checked">
<span class="color texture" style="background:brown;"></span>
</label>
</li>
<li class="float-xs-left input-container">
<label>
<input class="input-color" type="radio" data-product-attribute="17" name="group[17]" value="89">
<span class="color texture" style="background:red"></span>
</label>
</li>
<li class="float-xs-left input-container">
<label>
<input class="input-color" type="radio" data-product-attribute="17" name="group[17]" value="90">
<span class="color texture" style="background:yellow"></span>
</label>
</li>
</ul>
More information is needed
But you can give an ID to the part of your code that you want and when you click, it will be linked to the same part
you can use this code in js
document.location.reload(true)
or use this in js
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var scrollpos = localStorage.getItem('scrollpos');
if (scrollpos) window.scrollTo(0, scrollpos);
});
window.onbeforeunload = function(e) {
localStorage.setItem('scrollpos', window.scrollY);
};
</script>

Putting some 2 second breaks between questions in already made jquery code

I have a jquery code which is useful for changing one question for another. Questions them self are stored in div's. However, I am trying to work out a solution of how to make some small time brakes between these questions.
The only solution that I have at the moment is to hide/show each div with a question at specific time, but that's really bad code-wise, and I would like to have something that looks pretty and which could be easily changed (time-wise).
jQuery(function() {
var $els = $('div[id^=question]'),
i = 0,
len = $els.length;
$els.slice(1).hide();
setInterval(function() {
$els.eq(i).fadeOut(function() {
i = (i + 1) % len
$els.eq(i).fadeIn();
})
}, 15000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="question1"></div>
<div id="question2" class="question">
<input type="radio" name="q1" value="non" id="non" style="visibility: hidden" checked><br>
<label> <input type="radio" name="q1" value="1" id="guess" class="radio" >Rain</label>
<label> <input type="radio" name="q1" value="2" id="guess" class="radio" >No rain</label><br>
</div> <br>
<div id="question3" class="question">
<input type="radio" name="q2" value="non" id="non" style="visibility: hidden" checked><br>
<label> <input type="radio" name="q2" value="1" id="guess" class="radio" >Rain</label>
<label> <input type="radio" name="q2" value="2" id="guess" class="radio" >No rain</label><br>
</div> <br>
Take a look at my approach. I solved most of it with plain old CSS.
const getNextQuestion = (current) => {
let next = current.nextElementSibling;
if (next.tagName !== 'DIV') {
return;
}
return next;
}
let currentQuestion = document.querySelector('.is-open');
document.querySelector('button').addEventListener('click', () => {
let currentQuestion = document.querySelector('.is-open');
let nextQuestion = getNextQuestion(currentQuestion);
currentQuestion.classList.remove('is-open');
nextQuestion.classList.add('is-open');
currentQuestion = nextQuestion;
});
.question {
border: 1px solid red;
background: tomato;
margin-bottom: 25px;
border-radius: 25px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-family: 'Open Sans';
opacity: 0;
visibility: none;
transition: visibility 1s linear, opacity 1s linear;
}
.is-open {
opacity: 1;
visibility: visible;
}
<div>
<div class="question is-open">
Question 1
</div>
<div class="question">
Questi
<div>
<div class="question is-open">
Question 1
</div>
<div cla<div>
<div class="question is-open">
Question 1
</div>
<div class="question">
Question 2
</div>
</div>
<button>
Next Question
</button> ss="question"> Question 2
</div>
</div>
<button>
Next Question
</button> on 2
</div>
</div>
<button>
Next Question
</button>

Star-Rating System html

I'm currently working on a Guestbook and I would like to create a Star-Rating System. But I'm stuck! I just can't fix the stars so that they stay yellow(gelb) when I click on them.
This is my code so far:
<div class="rating">
<p id="rating-paragraph">Rate</p>
<img id="stern1" src="pictures/durchsichtig.png" onmouseover="stern1.src='pictures/gelb.png'" onmouseout="stern1.src='pictures/durchsichtig.png'" alt="error"> <?php ?>
<img id="stern2" src="pictures/durchsichtig.png" onmouseover="stern1.src='pictures/gelb.png'; this.src='pictures/gelb.png'" onmouseout="stern1.src='pictures/durchsichtig.png'; this.src='pictures/durchsichtig.png'" alt="error">
<img id="stern3" src="pictures/durchsichtig.png" onmouseover="stern1.src='pictures/gelb.png'; stern2.src='pictures/gelb.png'; this.src='pictures/gelb.png' " onmouseout="stern1.src='pictures/durchsichtig.png';stern2.src='pictures/durchsichtig.png'; this.src='pictures/durchsichtig.png' " alt="error">
<img id="stern4" src="pictures/durchsichtig.png" onmouseover="stern1.src='pictures/gelb.png'; stern2.src='pictures/gelb.png'; stern3.src='pictures/gelb.png'; this.src='pictures/gelb.png' " onmouseout="stern1.src='pictures/durchsichtig.png';stern2.src='pictures/durchsichtig.png'; stern3.src='pictures/durchsichtig.png';this.src='pictures/durchsichtig.png' " alt="error">
<img id="stern5" src="pictures/durchsichtig.png" onmouseover="stern1.src='pictures/gelb.png'; stern2.src='pictures/gelb.png'; stern3.src='pictures/gelb.png'; stern4.src='pictures/gelb.png'; this.src='pictures/gelb.png' " onmouseout="stern1.src='pictures/durchsichtig.png';stern2.src='pictures/durchsichtig.png'; stern3.src='pictures/durchsichtig.png';stern4.src='pictures/durchsichtig.png'; this.src='pictures/durchsichtig.png' " alt="error">
</div>
And css:
#stern1:hover, #stern2:hover, #stern3:hover, #stern4:hover, #stern5:hover{
cursor: pointer;
}
Thanks for your time and maybe for some help or advice :)
Pure CSS+HTML rating method without any images (works only with unicode charecter)
div.stars {
width: 270px;
display: inline-block;
}
input.star { display: none; }
label.star {
float: right;
padding: 10px;
font-size: 36px;
color: #444;
transition: all .2s;
}
input.star:checked ~ label.star:before {
content: '\2605';
color: #FD4;
transition: all .25s;
}
input.star-5:checked ~ label.star:before {
color: #FE7;
text-shadow: 0 0 20px #952;
}
input.star-1:checked ~ label.star:before { color: #F62; }
label.star:hover { transform: rotate(-15deg) scale(1.3); }
label.star:before {
content: '\2605';
}
<div class="stars">
<form action="">
<input class="star star-5" id="star-5" type="radio" name="star"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star"/>
<label class="star star-1" for="star-1"></label>
</form>
</div>

Strange alphabetical sorting of checkbox lists [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 6 years ago.
I have 4 checkboxes, each representing a region. Clicking any one of them shows 3 countries relevant to that region. Clicking combinations of the regional checkboxes shows all the relevant countries in-line, but I want
the list of country checkboxes to always be displayed in alphabetical order.
Strangely my jquery seemed to work for 3 regional checkboxes, but doesn't seem to work for 4. I just can't see what dumb mistake I'm making and am starting to suspect something more sinister. Here's my fiddle: https://jsfiddle.net/m5v7v6kv/
Thanks for any help.
function sortByText(a, b) {
return $.trim($(a).text()) > $.trim($(b).text());
}
var li = $(".CountryWrapper").children("label").detach().sort(sortByText)
$(".CountryWrapper").append(li)
$('input[type="checkbox"]').click(function() {
$('.my' + $(this).attr("id")).slideToggle(200)
})
.CountryWrapper {
border: 1px solid blue;
height: 150px;
width: 480px;
border: 1px solid blue;
}
.myEuropeCountries {
display: none;
}
.myNAMCountries {
display: none;
}
.mySAMCountries {
display: none;
}
.myAfricaMECountries {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input type="checkbox" id="EuropeCountries" />Europe</label>
<label><input type="checkbox" id="NAMCountries" />North America</label>
<label><input type="checkbox" id="SAMCountries" />South America</label>
<label><input type="checkbox" id="AfricaMECountries" />Africa and Middle East</label>
<div class="CountryWrapper">
<br>
<label class="myEuropeCountries"><input type="checkbox" value="Spain" />Spain</label>
<label class="myEuropeCountries"><input type="checkbox" value="Germany" />Germany</label>
<label class="myEuropeCountries"><input type="checkbox" value="Austria" />Austria</label>
<label class="myNAMCountries"><input type="checkbox" value="USA" />USA</label>
<label class="myNAMCountries"><input type="checkbox" value="Mexico" />Mexico</label>
<label class="myNAMCountries"><input type="checkbox" value="Canada" />Canada</label>
<label class="mySAMCountries"><input type="checkbox" value="Brazil" />Brazil</label>
<label class="mySAMCountries"><input type="checkbox" value="Argentina" />Argentina</label>
<label class="mySAMCountries"><input type="checkbox" value="Chile" />Chile</label>
<label class="myAfricaMECountries"><input type="checkbox" value="SouthAfrica" />South Africa</label>
<label class="myAfricaMECountries"><input type="checkbox" value="Egypt" />Egypt</label>
<label class="myAfricaMECountries"><input type="checkbox" value="SaudiArabia" />Saudi Arabia</label>
</div>
Looks like your compare function should return 1 or -1. There is really no reason to return 0 in your case unless somehow two countries will have the same name.
return $.trim($(a).text()) > $.trim($(b).text()) ? 1 : -1;
function sortByText(a, b) {
return $.trim($(a).text()) > $.trim($(b).text()) ? 1 : -1;
}
var li = $(".CountryWrapper").children("label").detach().sort(sortByText)
$(".CountryWrapper").append(li)
$('input[type="checkbox"]').click(function() {
$('.my' + $(this).attr("id")).slideToggle(200)
})
.CountryWrapper {
border: 1px solid blue;
height: 150px;
width: 480px;
border: 1px solid blue;
}
.myEuropeCountries {
display: none;
}
.myNAMCountries {
display: none;
}
.mySAMCountries {
display: none;
}
.myAfricaMECountries {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" id="EuropeCountries" />Europe</label>
<label>
<input type="checkbox" id="NAMCountries" />North America</label>
<label>
<input type="checkbox" id="SAMCountries" />South America</label>
<label>
<input type="checkbox" id="AfricaMECountries" />Africa and Middle East</label>
<!-------------------------------------------------------------------->
<div class="CountryWrapper">
<br>
<label class="myEuropeCountries">
<input type="checkbox" value="Spain" />Spain</label>
<label class="myEuropeCountries">
<input type="checkbox" value="Germany" />Germany</label>
<label class="myEuropeCountries">
<input type="checkbox" value="Austria" />Austria</label>
<label class="myNAMCountries">
<input type="checkbox" value="USA" />USA</label>
<label class="myNAMCountries">
<input type="checkbox" value="Mexico" />Mexico</label>
<label class="myNAMCountries">
<input type="checkbox" value="Canada" />Canada</label>
<label class="mySAMCountries">
<input type="checkbox" value="Brazil" />Brazil</label>
<label class="mySAMCountries">
<input type="checkbox" value="Argentina" />Argentina</label>
<label class="mySAMCountries">
<input type="checkbox" value="Chile" />Chile</label>
<label class="myAfricaMECountries">
<input type="checkbox" value="SouthAfrica" />South Africa</label>
<label class="myAfricaMECountries">
<input type="checkbox" value="Egypt" />Egypt</label>
<label class="myAfricaMECountries">
<input type="checkbox" value="SaudiArabia" />Saudi Arabia</label>
</div>

Toggle checkbox - click on parent DIV element

I have few div elements and inside every div one checkbox.
On click this div I want to check/uncheck checkbox and add class like active or checked. Have some code if you have any idea.
<div class="filter">
<div id="select_10">
<input type="checkbox" name="what" />
visible1
</div>
<div id="select_91">
<input type="checkbox" name="ever" />
visible2
</div>
<div id="select_101">
<input type="checkbox" name="whatever" />
visible3
</div>
</div>
using this code:
$(document).on("click", "div.filter div", function (event) {
var target = $(event.target);
var id = $(this)attr('id');
if (target.is('div#'+id+' input:checkbox')) {
return;
}
var checkbox = $(this).find("input[type='checkbox']");
checkbox.prop("checked", !checkbox.is(':checked'));
});
$(this)attr('id'); should have a . like in
$(this).attr('id');
That's it.
Why using DIV and JS when we have <label> for that purpose!
.filter label {
cursor: pointer;
display: block;
background: #eee;
margin:5px; padding: 10px;
}
<div class="filter">
<label id="select_10">
<input type="checkbox" name="what" />
visible1
</label>
<label id="select_91">
<input type="checkbox" name="ever" />
visible2
</label >
<label id="select_101">
<input type="checkbox" name="whatever" />
visible3
</label >
</div>
all you need. Style label as you would for your DIV by just adding eventually display: block;
Or you can even move the inputs before the label and style the complete LABEL elements in pure CSS:
.filter input{
position: absolute;
visibility: hidden;
}
.filter label {
cursor: pointer;
display: block;
margin:5px; padding: 10px;
background: #eee;
}
.filter input:checked + label{
background: #cf5;
}
.filter label:before{content: "\2610";}
.filter input:checked + label:before{content: "\2611";}
<div class="filter">
<input id="ckb_10" type="checkbox" name="what" />
<label for="ckb_10">
what
</label>
<input id="ckb_91" type="checkbox" name="foo" />
<label for="ckb_91">
foo
</label>
</div>

Categories