how to add value from range input to total amount? - javascript

I managed to summarize the values from the radio buttons, but I just can’t add the values from range inputs, how can this be implemented??
And yet, since the slider on the two range inputs initially has a value of 1, then in Sum: should already be 2.
$(document).ready(function() {
$(".in_objem").on('input', function() {
$(".range-indicator").text($(this).val());
});
$(".in_nagrev").on('input', function() {
$(".nagrev-indicator").text($(this).val());
});
// $(".in_objem").on('input', function() {
// var elem= parseInt($(this).val());
// });
$(".premium-inpts").on('click', function(){
var sum = 0;
$('.premium-inpts').each(function(){
if ($(this).is(':checked')){
sum = sum + parseInt($(this).next('.flexy-block').attr("price"));
}
});
console.log(sum);
$("#price-board").text(sum);
$("#output-price").val(sum);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div>
<span class="nagrev-indicator">1</span><span>units1</span>
</div>
<div>
<input class="in_nagrev" name="productivity1" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<div>
<span class="range-indicator">1</span><span>units2</span>
</div>
<div>
<input class="in_objem" name="productivity2" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<input class="premium-inpts" id="a" type="radio" value="work">
<label class="flexy-block" for="a" price="300">
<span>Work</span>
</label>
<input class="premium-inpts" id="b" type="radio" value="chill">
<label class="flexy-block" for="b" price="20">
<span>Chill</span>
</label>
</div>
<div class="motors-container">
<input class="premium-inpts" id="c" type="radio" value="Automatic">
<label class="flexy-block" for="c" price="90">
<span>Automatic</span>
</label>
<input class="premium-inpts" id="d" type="radio" value="Common">
<label class="flexy-block" for="d" price="0">
<span>Common</span>
</label>
</div>
<div class="price-board">Sum: <span id="price-board"></span></div>
<input id="output-price" name="Sum" type="hidden" value="">
Perhaps this is done somehow through an array, but I am not very good at js, so do not judge strictly.

You can sum the value of all the checked radio buttons with the slider value on the input event:
$(".in_objem,.in_nagrev").on('input', function() {
var elem = 0;
//sum the slider value
$(".in_objem,.in_nagrev").each(function(){
elem += Number($(this).val());
});
var v = 0;
//sum the checked radio buttons
$('.premium-inpts:checked').each(function(){
v = v + parseInt($(this).next('.flexy-block').attr("price"));
});
$("#price-board").text(v + elem);
$("#output-price").val(v + elem);
});
$(".in_objem,.in_nagrev").trigger('input'); // trigger the event to show the sum on page load
Working Code Example:
$(document).ready(function() {
$(".in_objem").on('input', function() {
$(".range-indicator").text($(this).val());
});
$(".in_nagrev").on('input', function() {
$(".nagrev-indicator").text($(this).val());
});
$(".in_objem,.in_nagrev").on('input', function() {
var elem = 0;
$(".in_objem,.in_nagrev").each(function(){
elem += Number($(this).val());
});
var v = 0;
$('.premium-inpts:checked').each(function(){
v = v + parseInt($(this).next('.flexy-block').attr("price"));
});
$("#price-board").text(v + elem);
$("#output-price").val(v + elem);
});
$(".in_objem,.in_nagrev").trigger('input');
$(".premium-inpts").on('click', function(){
var sum = 0;
var n1 = Number($('.in_objem').val());
var n2 = Number($('.in_nagrev').val());
sum = n1+n2;
$('.premium-inpts:checked').each(function(){
sum = sum + parseInt($(this).next('.flexy-block').attr("price"));
});
//console.log(sum);
$("#price-board").text(sum);
$("#output-price").val(sum);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div>
<span class="nagrev-indicator">1</span><span>units1</span>
</div>
<div>
<input class="in_nagrev" name="productivity1" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<div>
<span class="range-indicator">1</span><span>units2</span>
</div>
<div>
<input class="in_objem" name="productivity2" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<input class="premium-inpts" id="a" type="radio" value="work"> //this is value for form
<label class="flexy-block" for="a" price="300">
<span>Work</span>
</label>
<input class="premium-inpts" id="b" type="radio" value="chill">
<label class="flexy-block" for="b" price="20">
<span>Chill</span>
</label>
</div>
<div class="motors-container">
<input class="premium-inpts" id="c" type="radio" value="Automatic"> //this is value for form
<label class="flexy-block" for="c" price="90">
<span>Automatic</span>
</label>
<input class="premium-inpts" id="d" type="radio" value="Common">
<label class="flexy-block" for="d" price="0">
<span>Common</span>
</label>
</div>
<div class="price-board">Sum: <span id="price-board"></span></div>
<input id="output-price" name="Sum" type="hidden" value="">

You need to get the value of the range objects and parse them to numbers and add them in your sum:
$(document).ready(function() {
$(".in_objem").on('input', function() {
$(".range-indicator").text($(this).val());
});
$(".in_nagrev").on('input', function() {
$(".nagrev-indicator").text($(this).val());
});
//$(".in_objem").on('input', function() {
// var elem= parseInt($(this).val());
//});
$(".premium-inpts").on('click', function(){
var sum = 0;
$('.premium-inpts').each(function(){
if ($(this).is(':checked')){
sum = sum + parseInt($(this).next('.flexy-block').attr("price")) + parseInt($(".in_objem").val(),10)+ parseInt($(".in_objem").val(),10) ;
}
});
console.log(sum);
$("#price-board").text(sum);
$("#output-price").val(sum);
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div>
<span class="nagrev-indicator">1</span><span>units1</span>
</div>
<div>
<input class="in_nagrev" name="productivity1" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<div>
<span class="range-indicator">1</span><span>units2</span>
</div>
<div>
<input class="in_objem" name="productivity2" type="range" min="1" max="30" value="1">
</div>
</div>
<div>
<input class="premium-inpts" id="a" type="radio" value="work"> //this is value for form
<label class="flexy-block" for="a" price="300">
<span>Work</span>
</label>
<input class="premium-inpts" id="b" type="radio" value="chill">
<label class="flexy-block" for="b" price="20">
<span>Chill</span>
</label>
</div>
<div class="motors-container">
<input class="premium-inpts" id="c" type="radio" value="Automatic"> //this is value for form
<label class="flexy-block" for="c" price="90">
<span>Automatic</span>
</label>
<input class="premium-inpts" id="d" type="radio" value="Common">
<label class="flexy-block" for="d" price="0">
<span>Common</span>
</label>
</div>
<div class="price-board">Sum: <span id="price-board"></span></div>
<input id="output-price" name="Sum" type="hidden" value="">

As mentioned by aprouja1, you need to get their values. An easy way to do this, is by giving both range sliders an ID in HTML.(For Example slider1 & slider2).
Then use document.getElementById("slider1").value & document.getElementById("slider2").value to get their values. You can save these to a variable and calculate the sum this way.
let slider1Value = document.getElementById("slider1").value;
let slider2Value = document.getElementById("slider2").value;
sum = slider1Value + slider2Value;

Related

JQuery - sum check boxes values according parent class

I have script that sum all checked checkboxs, but I would like to sum only checkboxs values where parent div has class="show_list_item"
$('input:checkbox').change(function (){
var total_srvs_amount = 0;
$('input[name="amount"]:checkbox:checked').each(function(){
total_srvs_amount += isNaN($(this).val()) ? 0 : parseInt ($(this).val());
console.log(total_srvs_amount);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="show_list_item">
<input type="checkbox" name="amount" value="100">
<input type="checkbox" name="amount" value="120">
</div>
<div class="hide_list_item">
<input type="checkbox" name="amount" value="85">
<input type="checkbox" name="amount" value="90">
</div>
You can change the selector to apply the function on checkboxes inside .show_list_item:
$('.show_list_item input:checkbox').change(function ()
{
var total_srvs_amount = 0;
$('.show_list_item input[name="amount"]:checkbox:checked').each(function(){
total_srvs_amount += isNaN($(this).val()) ? 0 : parseInt ($(this).val());
console.log(total_srvs_amount);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="show_list_item">
<input type="checkbox" name="amount" value="100">
<input type="checkbox" name="amount" value="120">
</div>
<div class="hide_list_item">
<input type="checkbox" name="amount" value="85">
<input type="checkbox" name="amount" value="90">
</div>

Limit number of seat to be booked at a time

I have this simple table used for ticket reservation. It works fine but i want to improve it so a user can reserve 6 seats at a time if user need to add 7th seat should be alerted that he/she can book 6 seats at a time.
The other issue is how to make reserve button inactive if there is no any seat selected and make it active if one or more than one seats are selected?
const selections = {};
const inputElems = document.getElementsByTagName("input");
const totalElem = document.getElementById("total-container");
const hiddenTotalElem = document.getElementById("hidden-total");
const hiddenSeatElem = document.getElementById("hidden-seats");
const seatsElem = document.getElementById("selected-seats");
for (let i = 0; i < inputElems.length; i++) {
if (inputElems[i].type === "checkbox") {
inputElems[i].addEventListener("click", displayCheck);
}
}
function displayCheck(e) {
if (e.target.checked) {
selections[e.target.id] = {
id: e.target.id,
value: e.target.value
};
} else {
delete selections[e.target.id];
}
const result = [];
let total = 0;
for (const key in selections) {
result.push(selections[key].id);
total += parseInt(selections[key].value);
}
totalElem.innerText = total;
hiddenTotalElem.value = total;
seatsElem.innerHTML = result.join(",");
hiddenSeatElem.value = result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="container">
<h2>Please choose a seat to book</h2>
<form action="action_page.php" method="post">
<input type="checkbox" name="vehicle-a1" id="A1" value="100"><label for="A1">A1</label><br>
<input type="checkbox" name="vehicle-a2" id="A2" value="65"> <label for="A2">A2</label><br>
<input type="checkbox" name="vehicle-a3" id="A3" value="55"> <label for="A3">A3</label><br>
<input type="checkbox" name="vehicle-a4" id="A4" value="50"><label for="A4">A4</label><br>
<input type="checkbox" name="vehicle-b1" id="B1" value="100"><label for="B1">B1</label><br>
<input type="checkbox" name="vehicle-b2" id="B2" value="65"> <label for="B2">B2</label><br>
<input type="checkbox" name="vehicle-b3" id="B3" value="55"> <label for="B3">B3</label><br>
<input type="checkbox" name="vehicle-b4" id="B4" value="50"><label for="B4">B4</label><br>
<input type="checkbox" name="vehicle-c1" id="C1" value="100"><label for="C1">C1</label><br>
<input type="checkbox" name="vehicle-c2" id="C2" value="65"> <label for="C2">C2</label><br>
<input type="checkbox" name="vehicle-c3" id="C3" value="55"> <label for="C3">C3</label><br>
<input type="checkbox" name="vehicle-c4" id="C4" value="50"><label for="C4">C4</label><br>
<input type="hidden" name="total" id="hidden-total" value="0">
<input type="hidden" name="seats" id="hidden-seats" value="0">
<p id="demo">
Selected Seat(s)
<br>
<span id="selected-seats"></span>
<!-- container for selected seats -->
<br> Total: <span id="total-container"></span> USD
<button type="submit" class="btn btn-primary" name="submit">Reserve Now</button>
</p>
</form>
</div>
</body>
</html>
I just added a small code chunk:
if(result.length>6){
e.preventDefault();
console.log("Alreday got 6! Event prevented... Alert the user here.");
//Remove the property added in selections in the code above.
delete selections[e.target.id];
return;
}
Where if the result array gets to more than 6 items, you prevent the checkbox from being checked, alert the user in some way (as you prefer) and exit the function with return.
const selections = {};
const inputElems = document.getElementsByTagName("input");
const totalElem = document.getElementById("total-container");
const hiddenTotalElem = document.getElementById("hidden-total");
const hiddenSeatElem = document.getElementById("hidden-seats");
const seatsElem = document.getElementById("selected-seats");
for (let i = 0; i < inputElems.length; i++) {
if (inputElems[i].type === "checkbox") {
inputElems[i].addEventListener("click", displayCheck);
}
}
function displayCheck(e) {
if (e.target.checked) {
selections[e.target.id] = {
id: e.target.id,
value: e.target.value
};
} else {
delete selections[e.target.id];
}
const result = [];
let total = 0;
for (const key in selections) {
result.push(selections[key].id);
total += parseInt(selections[key].value);
}
// Enable the submit button if at least 1 checked
$(":submit").prop("disabled",!result.length>0);
if(result.length>6){
console.log("Alreday got 6! Event prevented... Alert the user here.");
e.preventDefault();
// Remove the property added in selections in the code above.
delete selections[e.target.id];
return;
}
totalElem.innerText = total;
hiddenTotalElem.value = total;
seatsElem.innerHTML = result.join(",");
hiddenSeatElem.value = result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="container">
<h2>Please choose a seat to book</h2>
<form action="action_page.php" method="post">
<input type="checkbox" name="vehicle-a1" id="A1" value="100"><label for="A1">A1</label><br>
<input type="checkbox" name="vehicle-a2" id="A2" value="65"> <label for="A2">A2</label><br>
<input type="checkbox" name="vehicle-a3" id="A3" value="55"> <label for="A3">A3</label><br>
<input type="checkbox" name="vehicle-a4" id="A4" value="50"><label for="A4">A4</label><br>
<input type="checkbox" name="vehicle-b1" id="B1" value="100"><label for="B1">B1</label><br>
<input type="checkbox" name="vehicle-b2" id="B2" value="65"> <label for="B2">B2</label><br>
<input type="checkbox" name="vehicle-b3" id="B3" value="55"> <label for="B3">B3</label><br>
<input type="checkbox" name="vehicle-b4" id="B4" value="50"><label for="B4">B4</label><br>
<input type="checkbox" name="vehicle-c1" id="C1" value="100"><label for="C1">C1</label><br>
<input type="checkbox" name="vehicle-c2" id="C2" value="65"> <label for="C2">C2</label><br>
<input type="checkbox" name="vehicle-c3" id="C3" value="55"> <label for="C3">C3</label><br>
<input type="checkbox" name="vehicle-c4" id="C4" value="50"><label for="C4">C4</label><br>
<input type="hidden" name="total" id="hidden-total" value="0">
<input type="hidden" name="seats" id="hidden-seats" value="0">
<p id="demo">
Selected Seat(s)
<br>
<span id="selected-seats"></span>
<!-- container for selected seats -->
<br> Total: <span id="total-container"></span> USD
<button type="submit" class="btn btn-primary" name="submit" disabled>Reserve Now</button>
</p>
</form>
</div>
</body>
</html>
EDIT
To enable the submit button, I added this:
$(":submit").prop("disabled",!result.length>0);
It is disabled by defaut in the HTML markup.
without jquery
document.addEventListener("DOMContentLoaded", function() {
const selections = {};
const inputElems = document.querySelectorAll('input[type=checkbox]');
const reserveButton = document.getElementById('reserveButton');
const totalElem = document.getElementById("total-container");
const hiddenTotalElem = document.getElementById("hidden-total");
const hiddenSeatElem = document.getElementById("hidden-seats");
const seatsElem = document.getElementById("selected-seats");
for (let i = 0; i < inputElems.length; i++) {
inputElems[i].addEventListener("click", displayCheck);
}
function displayCheck(e) {
if (e.target.checked) {
if (Object.keys(selections).length >= 6) {
e.preventDefault();
alert('You can only reserve 6 seats at time');
return;
} else {
selections[e.target.id] = {
id: e.target.id,
value: e.target.value
};
}
} else {
delete selections[e.target.id];
}
const result = [];
let total = 0;
for (const key in selections) {
result.push(selections[key].id);
total += parseInt(selections[key].value);
}
result.length ? reserveButton.disabled = false : reserveButton.disabled = true;
totalElem.innerText = total;
hiddenTotalElem.value = total;
seatsElem.innerHTML = result.join(",");
hiddenSeatElem.value = result;
}
});
<html>
<head>
</head>
<body>
<div class="container">
<h2>Please choose a seat to book</h2>
<form action="action_page.php" method="post">
<input type="checkbox" name="vehicle-a1" id="A1" value="100"><label for="A1">A1</label><br>
<input type="checkbox" name="vehicle-a2" id="A2" value="65"> <label for="A2">A2</label><br>
<input type="checkbox" name="vehicle-a3" id="A3" value="55"> <label for="A3">A3</label><br>
<input type="checkbox" name="vehicle-a4" id="A4" value="50"><label for="A4">A4</label><br>
<input type="checkbox" name="vehicle-b1" id="B1" value="100"><label for="B1">B1</label><br>
<input type="checkbox" name="vehicle-b2" id="B2" value="65"> <label for="B2">B2</label><br>
<input type="checkbox" name="vehicle-b3" id="B3" value="55"> <label for="B3">B3</label><br>
<input type="checkbox" name="vehicle-b4" id="B4" value="50"><label for="B4">B4</label><br>
<input type="checkbox" name="vehicle-c1" id="C1" value="100"><label for="C1">C1</label><br>
<input type="checkbox" name="vehicle-c2" id="C2" value="65"> <label for="C2">C2</label><br>
<input type="checkbox" name="vehicle-c3" id="C3" value="55"> <label for="C3">C3</label><br>
<input type="checkbox" name="vehicle-c4" id="C4" value="50"><label for="C4">C4</label><br>
<input type="hidden" name="total" id="hidden-total" value="0">
<input type="hidden" name="seats" id="hidden-seats" value="0">
<p id="demo">
Selected Seat(s)
<br>
<span id="selected-seats"></span>
<!-- container for selected seats -->
<br> Total: <span id="total-container"></span> USD
<button type="submit" class="btn btn-primary" name="submit" id="reserveButton" disabled>Reserve Now</button>
</p>
</form>
</div>
</body>
</html>
You can check if selections contains more than 6 keys when e.target.checked is true.
To inactive/active the button, you can do this
reserveBtn.disabled = result.length == 0;
const selections = {};
const inputElems = document.getElementsByTagName("input");
const totalElem = document.getElementById("total-container");
const hiddenTotalElem = document.getElementById("hidden-total");
const hiddenSeatElem = document.getElementById("hidden-seats");
const seatsElem = document.getElementById("selected-seats");
const reserveBtn = document.getElementById("reserve-button");
for (let i = 0; i < inputElems.length; i++) {
if (inputElems[i].type === "checkbox") {
inputElems[i].addEventListener("click", displayCheck);
}
}
function displayCheck(e) {
if (e.target.checked) {
if(Object.keys(selections).length>=6)
{
e.target.checked = false;
// alert goes here
// ...
return;
}
selections[e.target.id] = {
id: e.target.id,
value: e.target.value
};
} else {
delete selections[e.target.id];
}
const result = [];
let total = 0;
for (const key in selections) {
result.push(selections[key].id);
total += parseInt(selections[key].value);
}
totalElem.innerText = total;
hiddenTotalElem.value = total;
seatsElem.innerHTML = result.join(",");
hiddenSeatElem.value = result;
reserveBtn.disabled = result.length == 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="container">
<h2>Please choose a seat to book</h2>
<form action="action_page.php" method="post">
<input type="checkbox" name="vehicle-a1" id="A1" value="100"><label for="A1">A1</label><br>
<input type="checkbox" name="vehicle-a2" id="A2" value="65"> <label for="A2">A2</label><br>
<input type="checkbox" name="vehicle-a3" id="A3" value="55"> <label for="A3">A3</label><br>
<input type="checkbox" name="vehicle-a4" id="A4" value="50"><label for="A4">A4</label><br>
<input type="checkbox" name="vehicle-b1" id="B1" value="100"><label for="B1">B1</label><br>
<input type="checkbox" name="vehicle-b2" id="B2" value="65"> <label for="B2">B2</label><br>
<input type="checkbox" name="vehicle-b3" id="B3" value="55"> <label for="B3">B3</label><br>
<input type="checkbox" name="vehicle-b4" id="B4" value="50"><label for="B4">B4</label><br>
<input type="checkbox" name="vehicle-c1" id="C1" value="100"><label for="C1">C1</label><br>
<input type="checkbox" name="vehicle-c2" id="C2" value="65"> <label for="C2">C2</label><br>
<input type="checkbox" name="vehicle-c3" id="C3" value="55"> <label for="C3">C3</label><br>
<input type="checkbox" name="vehicle-c4" id="C4" value="50"><label for="C4">C4</label><br>
<input type="hidden" name="total" id="hidden-total" value="0">
<input type="hidden" name="seats" id="hidden-seats" value="0">
<p id="demo">
Selected Seat(s)
<br>
<span id="selected-seats"></span>
<!-- container for selected seats -->
<br> Total: <span id="total-container"></span> USD
<button type="submit" class="btn btn-primary" name="submit" id="reserve-button" disabled>Reserve Now</button>
</p>
</form>
</div>
</body>
</html>

Retain Multiple Radio Buttons checked after page refresh

In a form there are 2 group of radio buttons.
In the first group named Result, there are 4 option: id="ok", id="fa", id="fp", id="bp".
In the second group named ResultCategories, there are 9 option: id="cat1" .... id="cat9".
What I want:
a. If ok is clicked, ResultCategories will be unchecked (if already checked).
b. If fp or bp is clicked, cat9 of ResultCategories will be checked.
c. If one of the cat1 to cat8 of ResultCategories is clicked, fa of Result will be checked.
d. When radio buttons are clicked page will refresh and checked radio buttons remain checked.
I got a to c working but d is not working. It retains checked radio for only one group.
Here is what tried:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<fieldset class="scheduler-border">
<legend class="scheduler-border">Quality Check</legend>
<div style="float: right;">
<button type="submit" id="submit" name="submit" class="btn btn-info">Complete</button>
</div>
<br />
<br />
<br />
<div class="row">
<div class="column">
<div id="Result">
<label>Result:</label>
<label class="radioContainer">Ok
<input type="radio" name="Result" id ="ok" value="1" />
<span class="circle"></span>
</label>
<label class="radioContainer">Fasle Alarm
<input type="radio" name="Result" id="fa" value="2" />
<span class="circle"></span>
</label>
<label class="radioContainer">False Pass
<input type="radio" name="Result" id="fp" value="3" />
<span class="circle"></span>
</label>
<label class="radioContainer">Blatant Pass
<input type="radio" name="Result" id="bp" value="4" />
<span class="circle"></span>
</label>
</div>
</div>
<br />
<div class="column">
<div id="ResultCategories">
<label>Result Categories:</label>
<div>
<label class="radioContainer">Cat 1
<input type="radio" name="ResultCategories" id="cat1" value="1" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 2
<input type="radio" name="ResultCategories" id="cat2" value="2" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 3
<input type="radio" name="ResultCategories" id="cat3" value="3" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 4
<input type="radio" name="ResultCategories" id="cat4" value="4" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 5
<input type="radio" name="ResultCategories" id="cat5" value="5" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 6
<input type="radio" name="ResultCategories" id="cat6" value="6" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 7
<input type="radio" name="ResultCategories" id="cat7" value="7" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 8
<input type="radio" name="ResultCategories" id="cat8" value="8" />
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 9
<input type="radio" name="ResultCategories" id="cat9" value="9" />
<span class="circle"></span>
</label>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</body>
<script> $('input:radio').click(function() {
location.reload(); }); </script>
I have added a fiddle here: Fiddle
$(document).ready(function() {
var result_dom = $('input[name="Result"]');
var categories_dom = $('input[name="ResultCategories"]');
var cat9 = $('#cat9');
var fa = $('#fa')
result_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val == 1) {
categories_dom.prop('checked', false);
} else if (checked_val == 3 || checked_val == 4) {
cat9.prop('checked', true);
}
});
categories_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val >= 1 && checked_val <= 8) {
fa.prop('checked', true);
}
});
});
$(document).ready(function(){
if(localStorage.selected) {
$('#' + localStorage.selected ).attr('checked', true);
}
$('.radio').click(function(){
localStorage.setItem("selected", this.id);
});
});
How can I retain both group of radio buttons checked?
Updated Code:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var result_dom = $('input[name="Result"]');
var categories_dom = $('input[name="ResultCategories"]');
var cat9 = $('#cat9');
var fa = $('#fa')
result_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val == 1) {
categories_dom.prop('checked', false);
} else if (checked_val == 3 || checked_val == 4) {
cat9.prop('checked', true);
}
});
categories_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val >= 1 && checked_val <= 8) {
fa.prop('checked', true);
}
});
});$(document).ready(function() {
var result_dom = $('input[name="Result"]');
var categories_dom = $('input[name="ResultCategories"]');
var cat9 = $('#cat9');
var fa = $('#fa')
result_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val == 1) {
categories_dom.prop('checked', false);
} else if (checked_val == 3 || checked_val == 4) {
cat9.prop('checked', true);
}
});
categories_dom.on('change', function() {
var checked_val = $(this).val();
if (checked_val >= 1 && checked_val <= 8) {
fa.prop('checked', true);
}
});
});
$(document).ready(function(){
//get the selected radios from storage, or create a new empty object
var radioGroups = JSON.parse(localStorage.getItem('selected') || '{}');
//loop over the ids we previously selected and select them again
Object.values(radioGroups).forEach(function(radioId){
document.getElementById(radioId).checked = true;
});
//handle the click of each radio
$('.radio').on('click', function(){
//set the id in the object based on the radio group name
//the name lets us segregate the values and easily replace
//previously selected radios in the same group
radioGroups[this.name] = this.id;
//finally store the updated object in storage for later use
localStorage.setItem("selected", JSON.stringify(radioGroups));
});
});
</script>
</head>
<body>
<div class="container">
<form action="" method="POST">
<fieldset class="scheduler-border">
<legend class="scheduler-border">Quality Check</legend>
<div style="float: right;"><button type="submit" id="submit" name="submit" class="btn btn-info">Complete</button></div>
<br /> <br /> <br />
<div class="row">
<div class="column">
<div id="Result">
<label>Result:</label>
<label class="radioContainer">Ok
<input class="radio" type="radio" name="Result" id ="ok" value="1">
<span class="circle"></span>
</label>
<label class="radioContainer">Fasle Alarm <input class="radio" type="radio" name="Result" id="fa" value="2">
<span class="circle"></span>
</label>
<label class="radioContainer">False Pass <input class="radio" type="radio" name="Result" id="fp" value="3" >
<span class="circle"></span>
</label>
<label class="radioContainer">Blatant Pass <input class="radio" type="radio" name="Result" id="bp" value="4">
<span class="circle"></span>
</label>
</div>
</div>
<br />
<div class="column">
<div id="ResultCategories"><label>Result Categories:</label>
<div>
<label class="radioContainer">Cat 1 <input class="radio" type="radio" name="ResultCategories" id="cat1" value="1">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 2 <input class="radio" type="radio" name="ResultCategories" id="cat2" value="2">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 3 <input class="radio" type="radio" name="ResultCategories" id="cat3" value="3">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 4 <input class="radio" type="radio" name="ResultCategories" id="cat4" value="4">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 5 <input class="radio" type="radio" name="ResultCategories" id="cat5" value="5">
<span class="circle"></span>
</label> <label class="radioContainer">Cat 6 <input class="radio" type="radio" name="ResultCategories" id="cat6" value="6">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 7 <input class="radio" type="radio" name="ResultCategories" id="cat7" value="7">
<span class="circle"></span>
</label> <label class="radioContainer">Cat 8 <input class="radio" type="radio" name="ResultCategories" id="cat8" value="8">
<span class="circle"></span>
</label>
<label class="radioContainer">Cat 9 <input class="radio" type="radio" name="ResultCategories" id="cat9" value="9">
<span class="circle"></span>
</label>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</body>
<script>
$('input:radio').click(function() {
location.reload();
});
</script>
$(document).ready(function(){
//get the selected radios from storage, or create a new empty object
var radioGroups = JSON.parse(localStorage.getItem('selected') || '{}');
//loop over the ids we previously selected and select them again
Object.values(radioGroups).forEach(function(radioId){
document.getElementById(radioId).checked = true;
});
//handle the click of each radio
$('.radio').on('click', function(){
//set the id in the object based on the radio group name
//the name lets us segregate the values and easily replace
//previously selected radios in the same group
radioGroups[this.name] = this.id;
//finally store the updated object in storage for later use
localStorage.setItem("selected", JSON.stringify(radioGroups));
});
});

Calculate total on checkbox checked

HTML
<div id="catlist">
<input type="checkbox" id="cat_1" value="cat_1" price="1.5" /><label for="cat_1">cat_1</label><br/>
<input type="checkbox" id="cat_2" value="cat_2" price="2" /><label for="cat_2">cat_2</label><br/>
<input type="checkbox" id="cat_3" value="cat_3" price="3.5" /><label for="cat_3">cat_3</label><br/>
<input type="checkbox" id="cat_4" value="cat_4" price="4" /><label for="cat_4">cat_4</label><br/>
<input type="checkbox" id="cat_5" value="cat_5" price="5" /><label for="cat_5">cat_5</label><br/>
<input type="checkbox" id="cat_6" value="cat_6" price="6.5" /><label for="cat_6">cat_6</label><br/>
<input type="checkbox" id="cat_7" value="cat_7" price="7" /><label for="cat_7">cat_7</label><br/>
<input type="checkbox" id="cat_8" value="cat_8" price="8" /><label for="cat_8">cat_8</label><br/>
<input type="checkbox" id="cat_9" value="cat_9" price="9.5" /><label for="cat_9">cat_9</label>
</div>
<input type="text" id="total" value="0" />
Javascript
function calcAndShowTotal(){
var total = 0;
$('#catlist :checkbox[checked]').each(function(){
total =+ parseFloat($(this).attr('price')) || 0;
});
$('#total').val(total);
}
$('#pricelist :checkbox').click(function(){
calcAndShowTotal();
});
calcAndShowTotal();
I am getting particular value. WHY? I tried sum of total, i tried jquery but no success..
Use $('#catlist :checkbox:checked') selector to select checked check-boxes
[] is used as attribute selector and it could be used as '[type="checkbox"]' but it will not filter checked check-boxes
+ operator is not needed before parseFloat, it has to be total =+
Instead of calling handler, just invoke change handler using .change()
function calcAndShowTotal() {
var total = 0;
$('#catlist :checkbox:checked').each(function() {
total += parseFloat($(this).attr('price')) || 0;
});
$('#total').val(total);
}
$('#catlist :checkbox').change(calcAndShowTotal).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="catlist">
<input type="checkbox" id="cat_1" value="cat_1" price="1.5" />
<label for="cat_1">cat_1</label>
<br/>
<input type="checkbox" id="cat_2" value="cat_2" price="2" />
<label for="cat_2">cat_2</label>
<br/>
<input type="checkbox" id="cat_3" value="cat_3" price="3.5" />
<label for="cat_3">cat_3</label>
<br/>
<input type="checkbox" id="cat_4" value="cat_4" price="4" />
<label for="cat_4">cat_4</label>
<br/>
<input type="checkbox" id="cat_5" value="cat_5" price="5" />
<label for="cat_5">cat_5</label>
<br/>
<input type="checkbox" id="cat_6" value="cat_6" price="6.5" />
<label for="cat_6">cat_6</label>
<br/>
<input type="checkbox" id="cat_7" value="cat_7" price="7" />
<label for="cat_7">cat_7</label>
<br/>
<input type="checkbox" id="cat_8" value="cat_8" price="8" />
<label for="cat_8">cat_8</label>
<br/>
<input type="checkbox" id="cat_9" value="cat_9" price="9.5" />
<label for="cat_9">cat_9</label>
</div>
<input type="text" id="total" value="0" />
Using Array#reduce
function calcAndShowTotal() {
var total = [].reduce.call($('#catlist :checkbox:checked'), function(a, b) {
return a + +$(b).attr('price') || 0;
}, 0);
$('#total').val(total);
}
$('#catlist :checkbox').change(calcAndShowTotal).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="catlist">
<input type="checkbox" id="cat_1" value="cat_1" price="1.5" />
<label for="cat_1">cat_1</label>
<br/>
<input type="checkbox" id="cat_2" value="cat_2" price="2" />
<label for="cat_2">cat_2</label>
<br/>
<input type="checkbox" id="cat_3" value="cat_3" price="3.5" />
<label for="cat_3">cat_3</label>
<br/>
<input type="checkbox" id="cat_4" value="cat_4" price="4" />
<label for="cat_4">cat_4</label>
<br/>
<input type="checkbox" id="cat_5" value="cat_5" price="5" />
<label for="cat_5">cat_5</label>
<br/>
<input type="checkbox" id="cat_6" value="cat_6" price="6.5" />
<label for="cat_6">cat_6</label>
<br/>
<input type="checkbox" id="cat_7" value="cat_7" price="7" />
<label for="cat_7">cat_7</label>
<br/>
<input type="checkbox" id="cat_8" value="cat_8" price="8" />
<label for="cat_8">cat_8</label>
<br/>
<input type="checkbox" id="cat_9" value="cat_9" price="9.5" />
<label for="cat_9">cat_9</label>
</div>
<input type="text" id="total" value="0" />
Instead of looping you can use change which will respond to and change event on the checkbox. Also you need add or subtract value like this += for addition on -= for subtraction
var _total = 0;
$('input[type="checkbox"]').change(function() {
if($(this).is(':checked')){
_total += parseFloat($(this).attr('price')) || 0;
}
else{
_total -= parseFloat($(this).attr('price')) || 0;
}
$('#total').val(_total);
})
JSFIDDLE
$('input:checkbox').change(function(){
var totalprice=0;
$('input:checkbox:checked').each(function(){
totalprice+= parseFloat($(this).attr('price'));
});
$('#total').val(totalprice)
});

js radio buttons check

i am trying to add mutliple choice exam questions (like survey) to my site. For that i am using js and jquery. i added feature to make sure all group buttons have been checked. But the error is that I cannot resolve properly checking of selected ones. Js check only first if and if it is true then all questions have been answered true, if not then all answers are wrong.
here is my js code:
function doAjaxPost() {
var result = 4;
var rightAnswers = 0;
var allmarked = 0;
var response = "";
$('.answers').each(function() {
if ($(this).find('input[type="radio"]:checked').length > 0) {
allmarked = allmarked + 1;
} else {
alert("not all checked");
}
});
if (allmarked == result) {
allmarked = 0;
if ($("input[#name=7]:checked").val() == "right") {
rightAnswers = rightAnswers + 1;
}
if ($("input[#name=8]:checked").val() == "right") {
rightAnswers = rightAnswers + 1;
}
if ($("input[#name=9]:checked").val() == "right") {
rightAnswers = rightAnswers + 1;
}
if ($("input[#name=10]:checked").val() == "right") {
rightAnswers = rightAnswers + 1;
}
$('#info').html(rightAnswers + " / " + result);
}
}
here is my html:
<div class="clearfix single_content">
<div class="tom-right">
<h4 class="tom-right">1.4</h4>
<br />
<ul class="answers">
<input type="radio" name="9" value="1" id="9a" />
<label for="9a">1</label>
<br/>
<input type="radio" name="9" value="2" id="9b" />
<label for="9b">2</label>
<br/>
<input type="radio" name="9" value="3" id="9c" />
<label for="9c">3</label>
<br/>
<input type="radio" name="9" value="right" id="9d" />
<label for="9d">right</label>
<br/>
</ul>
</div>
</div>
<div class="clearfix single_content">
<div class="tom-right">
<h4 class="tom-right">1.5</h4>
<br />
<ul class="answers">
<input type="radio" name="10" value="1" id="10a" />
<label for="10a">1</label>
<br/>
<input type="radio" name="10" value="2" id="10b" />
<label for="10b">2</label>
<br/>
<input type="radio" name="10" value="3" id="10c" />
<label for="10c">3</label>
<br/>
<input type="radio" name="10" value="right" id="10d" />
<label for="10d">right</label>
<br/>
</ul>
</div>
</div>
i am stack to this point. it frankly seems stupid question but i am not spesialized in js. Thus, any assist would be appreciated
Because of this radio group selection, so you can only choose one; if you want to achieve that situation, you need to use checkbox to achieve;and the way to resolve ploblem is every options bind clickevent when clicking one of checkboxes,then the click func can achieve every option choosed
you can try this:
you can create an object with questions and related correct answers:
function doAjaxPost() {
var result = $('.answers').length;
var rightAnswers =0 ;
var response= "" ;
var error=false;
var correct_answers = [{question_number:1,answers_number:5},
{question_number:10,answers_number:2},
{question_number:9,answers_number:3}];
$('.answers').each(function(){
if($(this).find('input[type="radio"]:checked').length > 0){
var question_number=$(this).find('input[type="radio"]:checked').attr("name");
var answer_number =$(this).find('input[type="radio"]:checked').val();
$.each(correct_answers, function( index, value ) {
if(question_number==value.question_number && answer_number==value.answers_number)rightAnswers++;
});
}
else error=true;
});
if(error) alert("not all checked"); //display the error once
else $('#info').html(rightAnswers + " / " + result);
}
$('#check_values').click(function(){
doAjaxPost();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="clearfix single_content">
<div class="tom-right">
<h4 class="tom-right">1.4</h4> <br />
<ul class="answers">
<input type="radio" name="9" value="1" id="9a" />
<label for="9a">1</label><br/>
<input type="radio" name="9" value="2" id="9b" />
<label for="9b">2</label><br/>
<input type="radio" name="9" value="3" id="9c" />
<label for="9c">3</label><br/>
<input type="radio" name="9" value="4" id="9d" />
<label for="9d">4</label><br/>
</ul>
</div>
</div>
<div class="clearfix single_content">
<div class="tom-right">
<h4 class="tom-right">1.5</h4> <br />
<ul class="answers">
<input type="radio" name="10" value="1" id="10a" />
<label for="10a">1</label><br/>
<input type="radio" name="10" value="2" id="10b" />
<label for="10b">2</label><br/>
<input type="radio" name="10" value="3" id="10c" />
<label for="10c">3</label><br/>
<input type="radio" name="10" value="4" id="10d" />
<label for="10d">4</label><br/>
</ul>
</div>
</div>
<input type="button" id="check_values" value="Check"/>
<p id="info"></p>

Categories

Resources