Skip to content

Instantly share code, notes, and snippets.

@tingplenting
Last active April 2, 2019 06:16
Show Gist options
  • Save tingplenting/4f2f66d7a5bab32cb4b344fcb13e68c9 to your computer and use it in GitHub Desktop.
Save tingplenting/4f2f66d7a5bab32cb4b344fcb13e68c9 to your computer and use it in GitHub Desktop.
<input class="checkit" type="checkbox" name="daftar" value="Bike"> I have a bike<br>
<input class="checkit" type="checkbox" name="daftar" value="Car" checked> I have a car<br>
<input class="checkit" type="checkbox" name="daftar" value="Pen" checked> I have a Pen<br>
<input type="button" onclick="alert(getCheckedCheckboxesFor('daftar'));" value="Get Values" />
<button id="create">Create file</button>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>
<script>
function getCheckedCheckboxesFor(checkboxName) {
var checkboxes = document.querySelectorAll('input[name="' + checkboxName + '"]:checked'), values = [];
// Array.prototype.forEach.call(checkboxes, function(el) {
// values.push(el.value);
// });
values = "";
for(var i=0;i< checkboxes.length; i++){
values += checkboxes[i].value + "\n";
}
return values;
}
function myValues(selector) {
var checkboxArray = document.querySelectorAll(selector);
var myOutputText = "";
for(var i=0;i< checkboxArray.length; i++){
if (checkboxArray[i].checked) {
myOutputText += checkboxArray[i].value + "\n";
}
}
return myOutputText;
}
var textFile = null;
var makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(myValues('.checkit'));
link.style.display = 'block';
}, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment