Good evening and good night where ever you are, I am just a beginner at web developing and working on a little project for learning.
i have a problem (which I am sure it's very simple but i have no idea what should i do)
i have this code where i was trying to fill rate-stars depending on a value for each one:
$.fn.stars = function() {
return $(this).each(function() {
$(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
});
}
for (i = 1; i< 3 ; i++) {
var val = 'input[name=amount'+i+']';
var id = 'saj' + i;
var submit = '#sa' + i;
var extendedId = '#so' + i;
$(function() {
$(submit).click(function() {
$(extendedId).html('<span id = "'+id+'" class="stars">'+parseFloat($(val).val())+'</span>');
$('#'+id).stars();
});
$(submit).click();
});
}
span.stars, span.stars span {
display: block;
background: url(stars.png) 0 -16px repeat-x;
width: 80px;
height: 16px;
}
span.stars span {
background-position: 0 0;
}
.sajed {
display :none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class = "sajed" type="text" name="amount1" value="2.5" />
<input id = "sa1" class = "sajed" type="submit" value="update"/>
<p id = "so1" ><span >2.4618164</span></p>
<input class = "sajed" type="text" name="amount2" value="4" />
<input id = "sa2" class = "sajed" type="submit" value="update"/>
<p id = "so2"><span >2.4618164</span></p>
I got this code from internet, I needed to add it several times, it's working if I repeat html,css(with different class name), and jquery's functions.
what i wanted to do is to add the html code several times and use the same css and JQuery on it
I have to stars element, I am calling the above function inside a for loop but only the LAST element in the loop has the effect.
when i start the for loop from 0 to 1 just the FIRST stars got the shape when i start the for loop from 0 to 2 just the SECOND stars got the shape when i start the for loop from 0 to 3 NOTHING HAPPENS.
I am sorry for that bad illustration but i hope you got what i mean from the code and the next picture which has the result i got
the first one happens when the loop ends at index 1 and the second when it ends at 2 and the third when it ends at indexes bigger than 2