Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Both actions cannot be performed simultaneously

I'm trying to make a progress bar by using data-atribute, but in Jquery both actions cannot be performed simultaneously.

<div class="progressbar">
    <div class="progressvalue" data-percent="50"></div>
</div>
.progressbar {
    background: #9696A0;
    height: 20px;
    border-radius: 10px;
    color: #fff;
    text-align: center;
    font-size: 14px;
    position: relative;
    z-index: 3;
}
.progressvalue {
    background: #4650C8;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 0;
    z-index: -1;
    border-radius: 10px;
}
$('.progressvalue').each(function() {
    var $this = $(this);
    $this.css('width', $this.data('percent') + '%');
    $this.parent().text('tournament progress: ' + $this.data('percent') + '%');
});

The result is:

enter image description here

But it should be:

enter image description here

like image 552
Liimd Avatar asked Jul 29 '26 01:07

Liimd


1 Answers

You have two issues. Here's a working solution. https://jsfiddle.net/usyL2vcw/1/

Your first issue is that you are using this instead of $this. Your second issue is that you can't change the text of a div that has children (You can but it replaces the children). Use a sibling span instead.

<div class="progressbar">
    <div class="progressvalue" data-percent="50"></div>
    <span></span>
</div>

$('.progressvalue').each(function() {
    var $this = $(this);
    $this.css('width', $this.data('percent') + '%');
    $this.siblings().text('tournament progress: ' + $this.data('percent') + '%');
});
like image 197
Jack Avatar answered Jul 30 '26 14:07

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!