3

Please see my code so far (jsfiddle).

I am trying to change the color of the progress bar once it reaches max capacity.

How can I accomplish this?

HTML code:

<textarea></textarea>
<span id="characters">255 left</span>

<br>
<progress id="myProgress" value="0" max="255">
</progress>

JS code:

$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
     var max = 255;
    var cs = $(this).val().length;
    document.getElementById("characters").innerHTML= max-cs + " left..";
    document.getElementById("myProgress").value = cs;
    if (event.which < 0x20) {
        return;
      }
    if (cs == max) {
        event.preventDefault();
        // make the color of progress bar red here!! 
      }     

}
5
  • 1
    Possible duplicate of How to set color for CSS3 html5 progress element? Commented Nov 23, 2016 at 16:23
  • @hopkins-matt Not really, since there's no JS there. Commented Nov 23, 2016 at 16:24
  • @OmriAharon OP is asking how to change color. Color is changed using CSS. If OP doesn't know how to use JS to change CSS, then it would be a duplicate of stackoverflow.com/questions/566203/… Commented Nov 23, 2016 at 16:29
  • 1
    If you copy paste in multiple characters you can blow through your cs == max check. You probably will want to check if cs >= max and potentially do other checks to make sure you can't paste in text that would put you over your 255 limit. Commented Nov 23, 2016 at 16:32
  • @TJRockefeller thanks.. i didn't realize that. :) Commented Nov 23, 2016 at 17:49

4 Answers 4

9

add progress::-webkit-progress-value in css it changes color and also define it jquery.

      if(cs>=max) 
      $('#myProgress').css("background-color", "red");

or you can assign a class which will assign the background color like

      if(cs>=max) 
      $('#myProgress').addClass("red");

$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
	 var max = 255;
    var cs = $(this).val().length;
    if(cs>max) $(this).val($(this).val().toString().substring(0,max));
    document.getElementById("characters").innerHTML= max-cs + " left..";
    document.getElementById("myProgress").value = cs;
      if(cs>=max){
        $('#myProgress').addClass("red");
        }
      else{
        $('#myProgress').removeClass("red");

        }
    if (event.which < 0x20) {
        return;
      }
    if (cs >= max) {
        event.preventDefault();
        // make the color of progress bar red here!! 
      }     
      
}
progress.red{
  background-color:red;
  color:red;
  }
progress.red[value] {color:red} /* IE10 */
progress.red::-webkit-progress-bar-value {background-color:red}
progress.red::-webkit-progress-value {background-color:red}
progress.red::-moz-progress-bar {background-color:red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea></textarea>
<span id="characters">255 left</span>

<br>
<progress id="myProgress" value="0" max="255">
</progress>

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

4 Comments

could you please explain why css is needed there? i dont understand why progress::-webki-progress-bar-value.... is needed there for it to turn red.. with out the css it just turns green (which also i dont understand why)...
actually light blue(unfinished) and the green(finished atleast one time) are the default commbinations. so to override it we are using three browser standards webkit, moz, and IE(it uses color). thats why it shows the default value if you delete the css. -webkit-progress-bar actually denotes the background colour of the <progress>
Thank you. really liked the adding the class idea
I am glad that it that it helped :)
2

EDIT: So to be sure this works in most browsers you should use background-color over color.

Since you're already using JQuery:

$('#myProgress').css("background-color", "red");

pure JS:

document.getElementById('myProgress').style.backgroundColor = 'red';

1 Comment

In browser other than IE you should use 'background' or 'background-color' instead of 'color'
0

Just add

document.getElementById("myProgress").style.background = "#f3f3f3";

once you reach the max value

This should work fine

$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
     var max = 255;
    var cs = $(this).val().length;
    document.getElementById("characters").innerHTML= max-cs + " left..";
    document.getElementById("myProgress").value = cs;
    if (event.which < 0x20) {
        return;
      }
    if (cs == max) {
             document.getElementById("myProgress").style.background = "#f3f3f3";
        event.preventDefault();
        // make the color of progress bar red here!! 
      }     

}

1 Comment

This makes the progress color green for me. jsfiddle.net/aoqwvax2
0

Check updated snippet for this

$(document).on('keyup keydown', 'textarea', updateCount);

function updateCount(event) {
    var max = 255;
    var cs = $(this).val().length;
    $("#characters").html(max - cs + " left..");
    $("#myProgress").val(cs);
    if (event.which < 0x20 && cs == max) {
        return;
    }
    if (cs == max) {
        event.preventDefault();
        $('#myProgress').addClass("red");
    } else {
        $('#myProgress').removeClass("red");
    }
}
progress.red {
    background-color: red;
    color: red;
}
progress.red[value] {
    color: red
}
progress.red::-webkit-progress-bar-value {
    background-color: red
}
progress.red::-webkit-progress-value {
    background-color: red
}
progress.red::-moz-progress-bar {
    background-color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea></textarea>
<span id="characters">255 left</span>
<br>
<progress id="myProgress" value="0" max="255"></progress>

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.