function checkmysize(){
var values = [];
var inputs = $('.size-col');
for(var i = 0; i < inputs.length; i++){
values.push($(inputs[i]).val());
alert(values);
}
}
my intention is count the same value in my array , i know i had to check the length and the value of each and push into a array , but how i count the same value in my array?
lets said if i got 3 s and 1 m , i want it to alert to like this s=3 , m=1, L=0. can any one give me a help ?
-
1Possible duplicate of Counting the occurrences of JavaScript array elementsergonaut– ergonaut2015年11月03日 17:12:43 +00:00Commented Nov 3, 2015 at 17:12
1 Answer 1
function checkmysize() {
var values = [];
var inputs = $('.size-col');
for (var i = 0; i < inputs.length; i++) {
values.push($(inputs[i]).val());
}
alert(values);
}
checkmysize();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label>1.</label>
<select class="size-col">
<option value="s">S</option>
<option value="m">M</option>
<option selected value="l">L</option>
</select>
</div>
<div>
<label>2.</label>
<select class="size-col">
<option value="s">S</option>
<option selected value="m">M</option>
<option value="l">L</option>
</select>
</div>
<div>
<label>3.</label>
<select class="size-col">
<option value="s">S</option>
<option selected value="m">M</option>
<option value="l">L</option>
</select>
</div>
<div>
<label>4.</label>
<select class="size-col">
<option value="s">S</option>
<option value="m">M</option>
<option value="l">L</option>
</select>
</div>
answered Nov 3, 2015 at 17:17
Lucky Chingi
2,2561 gold badge13 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js