Change the width of the :after pseudo-element in CSS using jQuery

I've been attempting to dynamically adjust the width of an element using :after, but for some reason, my changes are not being applied. Can someone offer assistance with this issue?

$(function(){
var a=20;
$(".progress:after").width(a+"%"); 
// Despite setting it to 20%, the width still displays as 50% as defined in the 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>

For reference, you can also view the JSFiddle Link.

Answer №1

To implement the latest features, consider using the html5 progress element.

$(function() {
  var currentValue = 20;
  $("progress").prop('value', currentValue);
});
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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS Tips: Defining Pseudo-Element Sizes

Currently, I am trying to adjust the width of the :before pseudo-element in order to match it with the dimensions of the element itself, specifically an anchor tag. This is part of my goal to create an overlay that completely covers the entire element. As ...

Exploring the World of Observables within AngularJS

I've been experimenting with some exercises to grasp the inner workings of AngularJS, but I'm feeling a little lost at the moment. Despite scouring through the API, various documentation, and tutorials, I haven't quite found what I'm l ...

Enhance your visualization with precise zooming using the viewbox feature in Map

I am working on a map with various states, and I want to implement a feature where clicking on a state will zoom in to fill the screen. Currently, I am using Mapael with zoom enabled. However, when I specify a region in the code below, it zooms in but th ...

jQuery's .validate() function is not compatible with table rows that are created dynamically

I'm currently working on implementing the jQuery validation plugin to ensure that all inputs in my table are restricted between 0 and 10. Each row of the table is dynamically generated by Javascript. Below is my HTML table and JS code for adding a row ...

I am seeking assistance with generating a printed list from the database

Struggling for two full days to successfully print a simple list from the database. Take a look at the current state of the code: function CategoriesTable() { const [isLoading, setLoading] = useState(true); let item_list = []; let print_list; useEffect(( ...

Retrieve the data attribute from the last three tag names using the Jquery Prev() method

Hi there! I'm currently trying to map out the route for going from clicking a button on the ADDtab_138269500 and then navigating back (to the previous) all the way to the first encountered span tag but in reverse order. If you want to view the code, ...

Ensure the column breaks before the element without disrupting the wrapper in CSS

My objective is to always initiate a line break before a specific element, in this case, the h2 tag: https://i.sstatic.net/fy80H.png https://jsfiddle.net/hv0sm40o/3/ html <div class="columns"> <h2>Jelly pastry chocolate cake pastry</h2&g ...

Creating full-width bootstrap buttons within the navbar

Currently, I am utilizing bootstrap to design the navbar on my website. My goal is to have three buttons within the navbar that, when clicked, will navigate to other pages. Currently, my navbar looks like this: https://i.sstatic.net/31s1f.jpg However, th ...

"Enhancing user experience with jQuery UI tabs for selecting the best option

Does anyone know how to create jquery ui tabs within a selected option on a mobile version? I am having trouble with this and would appreciate any assistance. My design is responsive, with desktop versions using jquery tabs and the mobile version using sel ...

Steps for properly inserting a hyperlink:

I'm in need of assistance. I have a grid containing several images that I want to make clickable by adding anchor tags, but when I do this, the images seem to disappear completely. I suspect there's an issue with them being inside the container d ...

Hold off on taking any action until the user starts scrolling

I am trying to create a script that will show a div for 6 seconds and then hide it automatically if the user does not scroll. However, I am encountering some issues with achieving this functionality. var wasScrolled = false; $(window).on('scroll&apo ...

Learning React: Error - Unable to access the 'data' property when it is null

Currently, I am learning React by following a tutorial available at this link: http://facebook.github.io/react/docs/tutorial.html Specifically, I am focusing on the section related to fetching data from the server, which can be found here: http://facebook ...

What is the easiest way to display a div box when hovering over it?

Spent the entire night on this and still no solution... I have a link that, when hovered over by a user, should display a box (div) underneath it. The box should overlay anything that is beneath it. Does anyone know how to achieve this in the simplest way ...

The combination of absolute positioning and a canvas

Can someone help me understand why the span is positioned at the bottom of the root span instead of at the top? Here's a link to my Plunker: /* Styles go here */ span { background-color :#808080; } canvas { background-color:#800000; } <!DO ...

Even though I have clearly defined my indices, I am still encountering an error that remains unidentified

For some reason, this strip of code is functioning perfectly on one page but not on another... The code, form name attributes, and PHP functionality on the server side are all the same. I'm using jQuery ajax to call it. It works flawlessly when called ...

What is the best way to display a welcoming message only once upon visiting the home page?

After gaining experience working with rails applications for a few months, I have been tasked with adding a feature that displays a welcome message to users visiting the site home page for the first time, but not on subsequent visits or page reloads. What ...

Typing into an autocomplete suggestion influences the activation of the submit button

Currently, I am implementing the autocomplete functionality with the google.maps.places.Autocomplete API to allow users to easily find addresses. However, when a user selects an item from the dropdown using the keyboard and hits enter, it affects the submi ...

What is the best way to combine height and min-height in order to ensure that a div always extends to the bottom and keeps

I've tried multiple suggestions without success so far. In my website, I have a main section with white background centered on the page. It stretches nicely to the bottom using flex. See image: https://i.sstatic.net/RmJ2o.png However, there's a ...

How can I access the attributes of items stored in an array in JavaScript?

Input was being retrieved from an HTML text box and stored in an object with multiple properties. Two functions, addAddress and displayAddressDetails, were created for adding data and displaying it. However, there seems to be an issue with displaying the d ...

What is the reason behind Object.hasOwn(x,y) being different from Reflect.ownKeys(x).includes(y) when x represents a CSSStyleDeclaration object and y is a hyphenated property such as 'z-index'?

Both of these conditions are true: 'z-index' in getComputedStyle(document.body) // true Reflect.has(getComputedStyle(document.body), 'z-index') // true Additionally, the following statements also evaluate to true, indicating that &apo ...