4

I have a simple progress bar <progress value="22" id = "timerProgress"></progress>

I have managed to change its value using jQuery but I'd like to write text like Remaining : 10 sec INSIDE the progress bar.

I tried the innerHTML and the html properties but that didn't work out.

How to go about it?

1
  • The progress element can't have content like that Commented Mar 5, 2014 at 22:37

2 Answers 2

3

I think you have to add another element. I tried using a pseudo element on the progress bar, but for some reason it didn't work (only tried in Safari).

http://jsfiddle.net/smt6J/

HTML

<div id="progress">
    <progress max="100" value="22" class="progressBar"></progress>
    <div class="progressText">22% Done</div>
</div>

JQuery

$('#progress .progressBar').val(50);
$('#progress .progressText').text('50% Done');

CSS

#progress {
    position:relative;
}
#progress .progressBar {
    position: absolute;
    left:0;
    top:0;
}
#progress .progressText {
    position: absolute;
    left:0;
    top:0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising! Thanks! :) Will try it out.
0

You could use the 'before' or 'after' CSS pseudo element (NB: at this time, this approach appears to work in Chrome only):

progress:before {
  content:attr(value);
}

Or create another attribute and update that from your JavaScript:

<progress value="22" id="timerProgress" data-content="Remaining : 10 sec"></progress>

progress:before {
  content: attr(data-content);
}

var secondsRemaining = 10;
document.getElementById('timerProgress').setAttribute('data-content', 'Remaining : ' + secondsRemaining + ' sec');

Fiddle: http://jsfiddle.net/imcg/v4xEB/

4 Comments

Now, how would I go about changing text color and all that?
Added a fiddle to the answer. Does work (in Chrome), but not 100% sure about browser support
That doesn't work in Safari or Firefox for me. Does work in Chrome. I don't have IE installed, so I can't check.
@BarbaraLaird Yes you're right :before only seems to work in Chrome for the progress element. Going to add a warning. Thanks.

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.