i wonder if there is a more efficient way of counting the selections of a user:
This is the selection dropdown that is multiple times on the page:
<select class="span2 jquery-countable" name="category[7]">
<option></option>
<option value="1">1. vote</option>
<option value="2">2. vote</option>
<option value="3">3. vote</option>
<option value="4">4. vote</option>
</select>
This is my js that counts the selections:
$(document).ready(function () {
$('#selected-count').html('You choose ' + getCount() + ' entries');
function getCount() {
prio1 = $('.jquery-countable option:selected[value="1"]').length;
prio2 = $('.jquery-countable option:selected[value="2"]').length;
prio3 = $('.jquery-countable option:selected[value="3"]').length;
prio4 = $('.jquery-countable option:selected[value="4"]').length;
return prio1 + prio2 + prio3 + prio4;
}
$('.jquery-countable').change(function () {
$('.jquery-countable option:selected').each(function () {
$('#selected-count').html('You choose ' + getCount() + ' entries');
})
})
});
My goal is it to count all selections that where made by the user that are not empty?!
Is there a more efficient way?
Thanks!
i wonder if there is a more efficient way of counting the selections of a user:
This is the selection dropdown that is multiple times on the page:
<select class="span2 jquery-countable" name="category[7]">
<option></option>
<option value="1">1. vote</option>
<option value="2">2. vote</option>
<option value="3">3. vote</option>
<option value="4">4. vote</option>
</select>
This is my js that counts the selections:
$(document).ready(function () {
$('#selected-count').html('You choose ' + getCount() + ' entries');
function getCount() {
prio1 = $('.jquery-countable option:selected[value="1"]').length;
prio2 = $('.jquery-countable option:selected[value="2"]').length;
prio3 = $('.jquery-countable option:selected[value="3"]').length;
prio4 = $('.jquery-countable option:selected[value="4"]').length;
return prio1 + prio2 + prio3 + prio4;
}
$('.jquery-countable').change(function () {
$('.jquery-countable option:selected').each(function () {
$('#selected-count').html('You choose ' + getCount() + ' entries');
})
})
});
My goal is it to count all selections that where made by the user that are not empty?!
Is there a more efficient way?
Thanks!
Share Improve this question asked May 30, 2012 at 12:42 FMKFMK 1,1401 gold badge14 silver badges29 bronze badges 04 Answers
Reset to default 7This will return the number of selected items that is not empty as you wished:
function getCount(){
return $("select.jquery-countable option:selected[value!='']").length;
}
here is a way to do it
$(document).ready(function () {
$('#selected-count').html('You choose ' +0 + ' entries');
$('.jquery-countable').change(function () {
var count = 0;
$('.jquery-countable').each(function () {
if($.trim($(this).val()).length > 0)
count++;
});
$('#selected-count').html('You choose ' +count + ' entries');
})
});
Working Fiddle
Try something like this
$("select").change(function(){
var count = $("select.jquery-countable option:selected[value!='']").length;
console.log(count);
});
Yeah, just take the [value=#] off. The more generic selector will return an array of selected values, which should contain just 1 in the case of a select.
function getCount() {
return $('.jquery-countable option:selected').length;
}
This will count the total number of "selected" options on the page that are in a select with the class jquery-countable.