I have made a function to get the values of checked boxes and placing all values in an input field. But i am getting problem in the result in which i want to remove the last comma. I have tried slice but could not succeded. Please help me out.
<!DOCTYPE html>
<html>
<body>
<p>How would you like your coffee?</p>
<form name="myform" action="/action_page.php">
<input type="checkbox" name="coffee" value="100">With cream<br>
<input type="checkbox" name="coffee" value="150">With sugar<br>
<input type="checkbox" name="coffee" value="200">With milk<br>
<input type="checkbox" name="coffee" value="250">With tea<br>
<br>
<input type="button" onclick="myFunction()" value="Send order">
<br><br>
<input type="text" id="order" size="50">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
var coffee = document.forms["myform"];
var txt = "";
var i;
for (i = 0; i < coffee.length; i++) {
if (coffee[i].checked) {
txt = txt + coffee[i].value + ", ";
}
}
document.getElementById("order").value = "You ordered a coffee with: " + txt;
}
</script>
</body>
</html>
mapinstead of the for loop, and then use join.