I know this question has been asked many times, and I have found a few answers on how to get text()
from checkbox, but when I tried to get text only from checked one, is when my code has some errors.
Consider this code:
$('input.choose-age').on('change', function() {
$('input.choose-age').not(this).prop('checked', false);
});
<script src="http://ift.tt/1oMJErh"></script>
<div class="checkbox">
<label for='wom-10-14' class="checkbox-inline">
<input type="checkbox" id='wom-10-14' class='choose-age' checked />10-14
</label>
<label for='wom-15-19' class="checkbox-inline">
<input type="checkbox" id='wom-15-19' class='choose-age' />15-19
</label>
<label for='wom-10-19' class="checkbox-inline">
<input type="checkbox" id='wom-10-19' class='choose-age' />10-19
</label>
</div>
I want to select text()
from checkbox if is checked and save it into a var
. I am using d3.js
and mapbox
to run a mapDraw function, so I want to change indicators if a different checkbox is checked.
I know that this example returns the text, however is not what I'm looking for:
var ageLab = $('label[for=wom-10-14]').text();
This is what I tried:
1.
var ageLab = $('input:checkbox:checked').map(function() {
return $(this).prev('label.checkbox-inline').text();
}).get();
console.log(ageLab);
Source: example 1
It returns:
Array[1]
0: ""
length: 1
2
var ageLab = $('.choose-age:checked').text(this.checked ? $(this).text() : '');
Source: example 2
However, it doesn´t returns text.
Is there a better way to get text()
from checkbox when is checked.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire