0

Here`s the HTML Input Range

<input id="golden_range" type="range">

I can change the Background Color of the Range Track to "#DDD" using this CSS

#golden_range::-webkit-slider-runnable-track {
    width: 300px;
    height: 5px;
    background: #DDD;
    border: none;
    border-radius: 3px;
}
#golden_range::-moz-range-track {
    width: 300px;
    height: 5px;
    background: #DDD;
    border: none;
    border-radius: 3px;
}

but i'm not able to change the background color using Jquery/Javascript. Please help me with this. Here's the code i'm trying to.

$('#golden_range::-webkit-slider-runnable-track').css('background', '#000');
$('#golden_range::-moz-range-track').css('background', '#000');
4
  • Better to use a class anyway … Commented Mar 31, 2015 at 16:31
  • Is there any specific event when you want to change the color? Put it inside $(document).ready function Commented Mar 31, 2015 at 16:34
  • 1
    possible duplicate of Changing CSS pseudo-element styles via JavaScript Commented Mar 31, 2015 at 16:38
  • i want to dynamically change the background color of the range input depending on the value of this range input. That`s why I cannot use CSS classes. Commented Mar 31, 2015 at 16:44

4 Answers 4

2

You can do this now (2020) by making use of CSS variables.

CSS

#golden_range::-webkit-slider-runnable-track {
  background: var(--track-background, #ddd);
}

#golden_range::-moz-range-track {
  background: var(--track-background, #ddd);
}

Javascript

document.querySelector('#golden_range').style.setProperty('--track-background', '#f00');

(should change the track background to red)

I'm doing this in Player Chrome to style the media player progress bar, using a background gradient on the track to show progress.

Sign up to request clarification or add additional context in comments.

Comments

1

According to the code in your question, I see a mistake in your syntax. In css the background color property is written as "background-color: color_hex_value". This is how your css code should look like:

#golden_range::-webkit-slider-runnable-track {
  width: 300px;
  height: 5px;
  background-color: #DDD;
  border: none;
  border-radius: 3px;
}
  #golden_range::-moz-range-track {
  width: 300px;
  height: 5px;
  background-color: #DDD;
  border: none;
  border-radius: 3px;
}

And this is the code for jquery:

$("#golden_range::-webkit-slider-runnable-track").css("background-color", "#000");

In case you are still having doubts, give a look at these two sources for css background-color properties and Jquery css() method.

2 Comments

yeah, i initially tried this but its also not working.
So as already suggested, or try to have a look here: stackoverflow.com/questions/4481485/…
0

Bad news. I have not found a proper way to do... but I found a dirty way; Insert a style tag in the body of the page. Of course, the content of this tag is generated with javascript:

var estilo = $("<style>").attr("id", "some_id");
//for Chrome, e.g.
 var mod= "::-webkit-slider-runnable-track";

var txt= "#id_range" + mod + " {\n";
txt+= "    background-color: rgb(100,100,100)\n";
txt+= "}\n";

estilo.text(txt);

$("#some_id").remove();
$("div#other_id").append(estilo);

it's ugly, slow and bad, but it works.

Comments

0

Add below code into your files

CSS:

#golden_range.change::-webkit-slider-runnable-track {
    width: 300px;
    height: 5px;
    background: #000;
    border: none;
    border-radius: 3px;
}
#golden_range.change::-moz-range-track {
    width: 300px;
    height: 5px;
    background: #000;
    border: none;
    border-radius: 3px;
}

jQuery:

$(document).on('input', '#golden_range', function() {
       var rangeValue = $(this).val(); 
       var changeRange = 20;
        if(currrentRange > changeRange){
            $("#golden_range").addClass("change");
        }
        });

Note: Please check changeRange value and change as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.