0

I am trying to dynamically change the width of an element in :after. But it is not reflecting. Can someone please help!

$(function(){
var a=20;
$(".progress:after").width(a+"%"); 
// This is not showing 20%. Still shows 50% which was coded in CSS
});
.progress {
    width:200px;
    height:50px;
    border:1px solid black;
    position:relative;
}
.progress:after {
    content:'\A';
    position:absolute;
    background:black;
    top:0; bottom:0;
    left:0; 
    width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="progress"></div>

Here is the Link to JSFidle

13
  • 1
    You cannot use $(".progress:after") as a selector. Commented May 30, 2017 at 16:10
  • Question.. What is the purpose of the psuedo? Do you have dynamic content? Commented May 30, 2017 at 16:12
  • Look this stackoverflow.com/questions/29260296/… Commented May 30, 2017 at 16:13
  • Even better option: stackoverflow.com/questions/17788990/… Commented May 30, 2017 at 16:14
  • @Cam: I am trying to change the width so as to make it function like a progress bar. Commented May 30, 2017 at 16:14

1 Answer 1

1

You should use the, now available, html5 progress element.

$(function() {
  var a = 20;
  $("progress").prop('value', a);
});
progress {
    width:200px;
    height:50px;
    border:1px solid black;
    position:relative;
    background:transparent;
}
::-webkit-progress-bar{background:transparent;}

::-moz-progress-bar{background-color:black;}
::-ms-fill{background-color:black;}
::-webkit-progress-value{background-color:black;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<progress value="50" max="100"></progress>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.