12
I’m trying to create a progress bar a little different from the ones I found around. This for example, take the values
of the fields and plays in the aria-valuenow
from the progress bar.
In my case, I want to take the fields that are being filled and go calculating the percentage and updating the bar in real time.
I was able to do it using just one type of selector, but I can’t use more than one. See with just selects
:
$("select").one('change', function() {
var totalSelect = $('select').length;
console.log(totalSelect);
var atual = document.getElementById('progress').getAttribute('aria-valuenow');
console.log(atual);
if (atual == 0) {
var percentual = 100 / totalSelect;
console.log(percentual);
$('.progress-bar').css('width', percentual + '%').attr('aria-valuenow', percentual);
}
else {
percentual = (100 / totalSelect) + atual;
$('.progress-bar').css('width', percentual + '%').attr('aria-valuenow', percentual);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script><link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<form action="#" method="post">
<div class="container">
<br>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-3">
<label for="select1" class="control-label">
<select id="select1" class="form-control input-md">
<option>1</option>
<option>2</option>
</select>
</label>
<br><br>
<label for="select2" class="control-label">
<select id="select2" class="form-control input-md">
<option>1</option>
<option>2</option>
</select>
</label>
<br><br>
</div>
</div>
</div>
</form>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-9">
<div class="row">
<div class="col-md-9">
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" aria-valuenow="0" id="progress"
aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</div>
</div>
</div>
</div>
In the FIDDLE.
However, I want to do it with all the main form selectors: input
, select
, textarea
, radio
and checkbox
.
I tried something like this to put with input
also, but it didn’t work:
$("select, input").one('change', 'keypress', function() {
}
But it didn’t happen. I created an example below where I left off, with several fields, and to make it easier for those who want to help, I also created a FIDDLE.
$("select").one('change', function() {
var totalSelect = $('select').length;
var totalInput = $('input').length;
var total = totalInput + totalSelect;
console.log(total);
var atual = document.getElementById('progress').getAttribute('aria-valuenow');
console.log(atual);
if (atual == 0) {
var percentual = 100 / total;
console.log(percentual);
$('.progress-bar').css('width', percentual + '%').attr('aria-valuenow', percentual);
}
else {
percentual = (100 / total) + atual;
$('.progress-bar').css('width', percentual + '%').attr('aria-valuenow', percentual);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script><link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<form action="#" method="post">
<div class="container">
<br><br>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-3">
<label for="select1" class="control-label">
<select id="select1" class="form-control input-md">
<option>1</option>
<option>2</option>
</select>
</label>
<br><br>
<label for="select2" class="control-label">
<select id="select2" class="form-control input-md">
<option>1</option>
<option>2</option>
</select>
</label>
<br><br>
</div>
<div class="col-md-3">
<label for="input1" class="control-label">
<input id="input1" type="number" class="form-control">
</label>
<br><br>
<label for="input2" class="control-label">
<input id="input2" type="text" class="form-control">
</label>
<br><br>
</div>
<div class="col-md-3">
<label class="radio radio-inline" for="radioSim">
<input type="radio" class="radio" id="radioSim" name="radioName1" value="sim">
<b>Sim</b></label>
<label class="radio radio-inline" for="radioNao">
<input type="radio" class="radio" id="radioNao" name="radioName1" value="não">
<b>Não</b></label>
<br><br>
<label class="radio radio-inline" for="radioSim2">
<input type="radio" class="radio" id="radioSim2" name="radioName2" value="sim">
<b>Sim</b></label>
<label class="radio radio-inline" for="radioNao2">
<input type="radio" class="radio" id="radioNao2" name="radioName2" value="não">
<b>Não</b></label>
<br><br>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-3">
<label class="checkbox" for="Check1">
<input class="checkbox" type="checkbox" id="Check1"/>Check1
</label>
<label class="checkbox" for="Check2">
<input class="checkbox" type="checkbox" id="Check2"/>Check2
</label>
<label class="checkbox" for="Check3">
<input class="checkbox" type="checkbox" id="Check3"/>Check3
</label>
<br><br>
<label class="checkbox" for="Check1B">
<input class="checkbox" type="checkbox" id="Check1B"/>Check1B
</label>
<label class="checkbox" for="Check2B">
<input class="checkbox" type="checkbox" id="Check2B"/>Check2B
</label>
<label class="checkbox" for="Check3B">
<input class="checkbox" type="checkbox" id="Check3B"/>Check3B
</label>
</div>
<div class="col-md-3">
<label class="control-label" for="textArea1">TextArea1
<textarea class="form-control" id="textArea1" cols="80" rows="3"></textarea>
</label>
<br><br>
<label class="control-label" for="textArea2">TextArea2
<textarea class="form-control" id="textArea2" cols="80" rows="3"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-7"></div>
<label class="control-label">
<input type="submit" value="Enviar"/>
</label>
</div>
</div>
</form>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-9">
<div class="row">
<div class="col-md-9">
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" aria-valuenow="0" id="progress"
aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</div>
</div>
</div>
</div>
wouldn’t be something like:
$("select, input").one('change keypress', function() {
– Chun
Ah, that’s right! I had already turned off the note, then I saw by cell phone and thought WTF is not possible... kkkk called back and bingo. Thanks! Now I need to think about some details, because when you choose a radio should be x2 (because you passed two fields), and when you choose in checkbox can be up to more than 2... and it seems that for textarea does not work just put input... tomorrow I’ll see better, thanks for now! @Chun
– gustavox
Ah, and for the record, I also had to put the
parseInt
in the variables...– gustavox
hehehe xD yes, you have to add also the
textarea
as selector and so on. I was thinking of another way to approach that, instead of pointing to the selects, create a class responsible for that, calculate how many elements exist with this class and add 1 value to each element found in the document to then calculate the total value and display the percentage in the progress bar, but I have not yet mentally constructed how to start developing. And then I’m afraid it might not be quite this way that you want to do things :P– Chun
Yeah, before bed I thought, "Jeez, just add the textarea!" hehe I think it would work with same class right, would only give more work to include a common class to all... and I do not know very well how it would work, because in this case, I understood, each expected behavior is added in the same sequence,
select
is with thechange
,textarea
andinput
stayskeypress
andkeypress
etc... I think this way will already solve [continue]– gustavox
But there are some details that I’ll still need help with... then I’ll create a fiddle to help you answer :D... thanks! @Chun
– gustavox
So I actually realized that I didn’t even need to be using the
keypress
, this was causing the increases occur in triples when it was intext
ortextarea
. I just thoughtchange
functioned only for theselects
... But it’s coming out, I also modified the count, already dividing theradios
for 2, I have just not been able to resolve the issue ofcheckboxes
, because in this case it is possible to choose more than one, but it is not necessary. So I’d like when I pick one, I’m going to advance the total of that group of checkboxes, and if I pick another, I’m not going to advance at all...– gustavox
Here is the updated fiddle: https://jsfiddle.net/t8qzd4c1/4/ E @Chun I await your answer, even if only with the solution you have already posted as a comment, but if you can solve this question of checkboxes I would be grateful double! rsrs And look, if you want to present another solution I would love to see... but I wanted to be able to implement this way if only for the challenge (and I use later your solution hehe)... Thanks!
– gustavox
Yeah, that part of selecting several
checkboxes
but just count as 1 also do not know how to solve, maybe the more experienced can help you in this. I would even add that comment as a response, but since it would be a response of just a few lines, I don’t think it’s worth posting it as an answer. This solution is better than the one I had in mind. But now I’m also interested to know how that part ofcheckboxes
will work :)– Chun
@gustavox needs to be an answer using Bootstrap?
– Renan Gomes
Not @Renan. It might be without bootstrap... but in this case I think it would be relatively simple to adapt your solution (JS mainly) to Bootstrap, or not? Anyway you don’t need to consider boostrap.
– gustavox
So @Renan and that other question is why some fields will be hidden and/or optional, so I thought I would apply a class to these options, and when they were modified the account would modify. Because otherwise, when these fields open the full bar before time...
– gustavox
I’m already going to take a look there @gustavox :)
– Renan Gomes