I am working on a jQuery ratings function that uses the Foundation range slider. My current implementation works correctly on 2 of 7 of my range sliders. However, I am quickly realising that my implementation will become bloated as I have to repeat myself for each of the 7 range sliders.
HTML:
<div class="row">
<div class="small-8 medium-8 columns">
<div class="range-slider" data-slider data-options="start: 1; end: 5;">
<span class="range-slider-handle" role="slider" tabindex="0"></span>
<span class="range-slider-active-segment"></span>
<%= f.hidden_field :quality %>
</div>
</div>
<div class="small-4 medium-4 columns">
<span id="sliderOutput1">Average</span>
</div>
</div>
<div class="row">
<div class="small-8 medium-8 columns">
<div class="range-slider" data-slider data-options="start: 1; end: 5;">
<span class="range-slider-handle" role="slider" tabindex="0"></span>
<span class="range-slider-active-segment"></span>
<%= f.hidden_field :communication %>
</div>
</div>
<div class="small-4 medium-4 columns">
<span id="sliderOutput2">Average</span>
</div>
</div>
jQuery:
$(document).on("page:load ready", function() {
function ratings(selector, callback) {
var input = $(selector);
var oldvalue = input.val();
setInterval(function(){
if (input.val()!= oldvalue){
oldvalue = input.val();
callback();
}
}, 100);
}
ratings('input#agent_score_quality', function() {
var inputValue = $('input#agent_score_quality').val();
if(inputValue === '1') { // Terrible
$("span#sliderOutput1").fadeOut(function() {
$(this).text('Terrible').fadeIn();
});
}
if(inputValue === '2') { // Poor
$("span#sliderOutput1").fadeOut(function() {
$(this).text('Poor').fadeIn();
});
}
if(inputValue === '3') { // Average
$("span#sliderOutput1").fadeOut(function() {
$(this).text('Average').fadeIn();
});
}
if(inputValue === '4') { // Good
$("span#sliderOutput1").fadeOut(function() {
$(this).text('Good').fadeIn();
});
}
if(inputValue === '5') { // Excellent
$("span#sliderOutput1").fadeOut(function() {
$(this).text('Excellent').fadeIn();
});
}
});
ratings('input#agent_score_communication', function() {
var inputValue = $('input#agent_score_communication').val();
if(inputValue === '1') { // Terrible
$("span#sliderOutput2").fadeOut(function() {
$(this).text('Terrible').fadeIn();
});
}
if(inputValue === '2') { // Poor
$("span#sliderOutput2").fadeOut(function() {
$(this).text('Poor').fadeIn();
});
}
if(inputValue === '3') { // Average
$("span#sliderOutput2").fadeOut(function() {
$(this).text('Average').fadeIn();
});
}
if(inputValue === '4') { // Good
$("span#sliderOutput2").fadeOut(function() {
$(this).text('Good').fadeIn();
});
}
if(inputValue === '5') { // Excellent
$("span#sliderOutput2").fadeOut(function() {
$(this).text('Excellent').fadeIn();
});
}
});
});
How can I improve this code so I don't have to repeat the same thing over and over just with different selectors? My idea is to use a single selector for the sliderOutput
and use the .closest()
method to target each and insert my text, but I'm new to jQuery, so could I be way off?