JQuery: Calculation & Variable is not Displaying When Checkbox Checked - javascript

Still learning JQuery. Still new to JQuery in fact. Trying to code is:
When the an items checkbox is checked, it calculates the service fee times years. then add the license fee to it. Finally it takes the total and times the quantity, which in then creates the item_cost.
If more than one item is checked, it will also calculate the item_cost and adds each checked item's cost, creating a total_item_cost. And, it will add each checked item's training hours, creating a total_training_hours.
Once item is done calculating, it will then display the item name, years, quantity, training hours, and total cost of each item that is checked at the bottom in the table. It will also display another table with total training hours and the the total cost of all items.
I have coded below but cannot seem to get the items to show in the table.
<script>
$(document).ready(function(){
//First obtaining indexes for each checkbox that is checked
$('input[id^=item_add]').change(function(){
// get checkbox index
var index = $(this).attr('id').replace('item_add','');
//hide tables until box is checked
$('table[id^=add_item_here]').hide();
$('table[id^=add_totals_here]').hide();
//If checkbox is checked, show tables, calculate item cost, get variables, calculate total cost, & calculate total training hours
$('input:checkbox').change(function(){
//If check box is check do...
if($(this).is(':checked')){
// Show totals tables
$('#add_item_here').show();
$('#add_totals_here').show();
// start at 0 for item_cost
var item_cost = 0;
var total_cost = 0;
$('input:checkbox:checked').each(function(){
// Calculating item cost for just that one checkbox
item_cost+=parseInt($('#servicefee'+index).val());
item_cost*=parseInt($('#yrs'+index).val());
item_cost+=parseInt($('#licensefee'+index).val());
item_cost*=parseInt($('#qty'+index).val());
// Get hidden variables to use for calculation and tables.
var item = $('#item'+index).val();
var traininghrs = parseInt($('#traininghrs'+index).val());
var qty = parseInt($('#qty'+index).val());
var yrs = parseInt($('#yrs'+index).val());
} else {
//if not checked keep tables hidden
$('#add_item_here').hide();
$('#add_totals_here').hide();
}
// start at 0 for total_costs
var total_cost = item_cost;
//Add item_cost with other item_costs to calculate total_cost
$('input:checkbox:checked').each(function(){
total_cost+= item_cost;
}
// start at 0 for total training hours
var total_training = 0;
//Add trianing hours with other training hours to calculate total_training
$('input:checkbox:checked').each(function(){
total_training+=parseInt($('#traininghrs'+index).val());
}
});
//Display each item that is checked into a table
$('#add_item_here tr:last').append('<td>' + item +'</td><td>' + yrs +'</td><td>' + qty +'</td><td>' + traininghrs + '</td><td>'+ item_cost + '</td></tr><tr>');
//Display totals into table row into a table
$('#add_totals_here tr:last').append('<td colspan=3 ></td><td>' + total_training + '</td><td>'+ total_cost + '</td></tr><tr>');
});
// Quantity change, if someone changes the quantity
$('#qty'+index).change(function(){
$('input:checkbox').trigger('change');
});
// Years change, if someone changes the years
$('#yrs'+index).change(function(){
$('input:checkbox').trigger('change');
});
}
}
</script>
Need to have each items and their details shown in a table and then need to thave a total table. So far the table format to display looks like:
<table id="add_item_here" style="width:98%;">
<tr><td></td></tr>
</table>
<table id="add_totals_here" style="width:98%;">
<tr><td></td></tr>
</table>
As requested, below is the HTML Form itself.
<form id="myForm" method="post" class="price-quote-form" action="#" >
<div class="tx-row" style="width:100%; float:left; clear:right;">
<div class="tx-column tx-column-size-2-3" style="float:left; margin-top:-20px; ">
<label>
<strong>Jurisdiction / Organization:</strong> <span style="font-size:10px; color:#ff0000;">(required)</span><br />
<input style= "width:90%; height:35px; font-size:18px; border: 1px #396ba5 solid;" type="text" name="jurisdiction" required>
</label>
</div>
<div class="tx-column tx-column-size-1-3" style="float:left; margin-top:-20px; ">
<label>
<strong style="font-size:14px;">State:</strong> <span style="font-size:10px; color:#ff0000;">(required)</span><br />
<select style= "width:90%; height:35px; font-size:18px; border: 1px #396ba5 solid;" id="state" name="state" required>
<option value="" >Select State</option>
<option value="IlIaNd" >Iowa</option>
<option value="IlIaNd" >Illinois</option>
<option value="Minnesota" >Minnesota</option>
<option value="Missouri" >Missouri</option>
<option value="Nebraska" >Nebraska</option>
<option value="IlIaNd" >North Dakota</option>
<option value="SouthDakota" >South Dakota</option>
</select>
</label>
</div>
</div>
<script>
$(document).ready(function(){
$("#state").change(function(){
$(this).on('change', function() {
if(this.value =="IlIaNd"){
$(".box").not(".IlIaNd").hide();
$(".IlIaNd").show();
}else if(this.value =="Minnesota"){
$(".box").not(".Minnesota").hide();
$(".Minnesota").show();
}else if(this.value =="Missouri"){
$(".box").not(".Missouri").hide();
$(".Missouri").show();
}else if(this.value =="Nebraska"){
$(".box").not(".Nebraska").hide();
$(".Nebraska").show();
}else if(this.value =="SouthDakota"){
$(".box").not(".SouthDakota").hide();
$(".SouthDakota").show();
}else{
$(".box").hide();
}
});
}).change();
});
</script>
<div style="width: 100%; float:left; clear:both; height:25px;" > </div>
<div style="border-bottom: 1px solid #cccccc; width: 100%; float:left; clear:both;" > </div>
<div style="width: 100%; float:left; clear:both; height:25px;" > </div>
<div style="width: 100%; float:left; clear:both;" class="IlIaNd box">
<?php IlIaNd_Price_Quote_Form() ?>
</div>
<div style="width: 100%; float:left; clear:both;" class="Minnesota box" >
<p>test 2</p>
</div>
<div style="width: 100%; float:left; clear:both;" class="Missouri box">
<p>test 3</p>
</div>
<div style="width: 100%; float:left; clear:both;" class="Nebraska box" >
<p>test 4</p>
</div>
<div style="width: 100%; float:left; clear:both;" class="SouthDakota box">
<p>test 5</p>
</div>
<center>
<table id="add_item_here" style="width:98%;">
<tbody>
<tr><td>Item</td><td>Years</td><td>Quantity</td><td>Training Hours</td><td>Total Item Cost</td></tr>
</tbody>
</table>
</center>
<center>
<table id="add_totals_here" style="width:98%;">
<tbody>
<tr><td cospan=3></td><td>Total Cost</td></tr>
</tbody>
</table>
</center>
</form>
Here is the PHP Portion of the form, which I am grabbing the items and pricing from a database, and then create checkboxes with the item name, then I have the prices of both fees below it. And I also have a drop down on either how many years and / or quantity.
if (pg_num_rows($result) > 0){
echo "<div class=\"tx-row\" style=\width:100%; clear:both; \" > <div style=\"width: 100%; float:left; clear:both; height:25px;\" > </div>";
while ($row = pg_fetch_object($result)){
$contractid = $row->id;
$item = $row->descr;
$license_fee = $row->license;
$service_fee = $row->service;
$hours = $row->training;
echo "<div class=\"tx-column tx-column-size-1-3\" >
<div id='item".$row->contractid."' style='width:100%;'>
<div style='width:10%; float:left;'>
<input style='height:25px; width:25px;' type='checkbox' id='item_add".$row->id."' name='item_add' value='add' />
</div>
<div style='width:90%; float:left;'>
<p style='width:115%;'><strong>$item</strong> <br /><span style='font-size:10px;'>Service Fee: $$service_fee | License Fee: $$license_fee</span></p>";
if ($contractid == "8" || $contractid == "10" ){
echo "<div style='width:100%; margin-top:-20px;'>
<div style='width:50%; float:left;'>
<span style='font-size:12px;'>Years: </span><select style='height:18px;' name='yrs' id='yrs".$row->id."'>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</div>
<div style='width:50%; float:left;'>
<span style='font-size:12px;'>Quantity: </span><select name='qty' id='qty".$row->id."' style='height:18px;'>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
</div>
<input type='hidden' name='item' id='item".$row->id."' value='".$row->descr."'>
<input type='hidden' name='servicefee' id='servicefee".$row->id."' value='$service_fee'>
<input type='hidden' name='licensefee' id='licensefee".$row->id."' value='$license_fee'>
<input type='hidden' name='traininghrs' id='traininghrs".$row->id."' value='$hours'>
</div>
<div style='width:100%; height:25px; clear:both; float:left;'></div>";
} else {
echo "<div style='width:100%; margin-top:-20px;'>
<span style='font-size:12px;'>Years: </span><select name='yrs' id='yrs".$row->id."' style='height:18px;'>
<option value=\"1\">1</option>
<option value=\"2\">2</option>
<option value=\"3\">3</option>
<option value=\"4\">4</option>
<option value=\"5\">5</option>
</select>
<input type='hidden' name='Qty' id='qty".$row->id."' value='1' >
<input type='hidden' name='item' id='item".$row->id."' value='".$row->descr."'>
<input type='hidden' name='servicefee' id='servicefee' value='".$row->service."'>
<input type='hidden' name='licensefee' id='licensefee".$row->id."' value='".$row->license."'>
<input type='hidden' name='traininghrs' id='traininghrs".$row->id."' value='".$row->training."'>
</div>
<div style='width:100%; height:25px; clear:both; float:left;'></div>";
}
echo "</div>";
echo "</div> ";
echo "</div>";
}
echo "</div>";
}
Hope adding the php and the html form helps anyone who can solve this out.
Here is the fiddle
https://jsfiddle.net/e9mmn55k/5/

$(document).ready(function(){
$('table[id^=add_item_here]').hide();
$('table[id^=add_totals_here]').hide();
//First obtaining indexes for each checkbox that is checked
$('input[name=item_add]').change(function(){
var index = this.id.replace('item_chk','');
if($(this).is(':checked')){
AddNewItem(index);
}
else{
RemoveItem(index);
}
CalculateTotals();
});
function AddNewItem(index){
// Get hidden variables to use for calculation and tables.
var item = $('#item_chk'+index).parent().text().trim();
var traininghrs = parseInt($('#traininghrs'+index).val());
var qty = parseInt($('#qty'+index).val());
var yrs = parseInt($('#yrs'+index).val());
var item_cost = 0;
// Calculating item cost for just that one checkbox
item_cost+=parseInt($('#servicefee'+index).val());
item_cost*=parseInt($('#yrs'+index).val());
item_cost+=parseInt($('#licensefee'+index).val());
item_cost*=parseInt($('#qty'+index).val());
var traininghrs = parseInt($('#traininghrs'+index).val());
//Display each item that is checked into a table
$('#add_item_here tr:last').after('<tr id="row_id'+index + '"><td>' + item +'</td><td>' + yrs +'</td><td>' + qty +'</td><td>' + traininghrs + '</td><td>'+ item_cost + '</td></tr>');
}
function RemoveItem(index){
$('table#add_item_here tr#row_id'+index).remove();
}
function CalculateTotals(){
var total_cost = 0;
var total_training = 0;
$('input:checkbox:checked').each(function(){
var index = this.id.replace('item_chk','');
var item_cost = 0;
// Calculating item cost for just that one checkbox
item_cost+=parseInt($('#servicefee'+index).val());
item_cost*=parseInt($('#yrs'+index).val());
item_cost+=parseInt($('#licensefee'+index).val());
item_cost*=parseInt($('#qty'+index).val());
var traininghrs = parseInt($('#traininghrs'+index).val());
total_cost +=item_cost;
total_training +=traininghrs;
});
if(total_cost > 0 || total_training > 0) {
$('#add_totals_here tr:has(td)').remove();
$('#add_totals_here tr:has(th)').after('<tr><td colspan="3">' + total_training + '</td><td>'+ total_cost + '</td></tr>');
$('#add_item_here').show();
$('#add_totals_here').show();
}
else{
$('table[id^=add_item_here]').hide();
$('table[id^=add_totals_here]').hide();
}
}
// Quantity change, if someone changes the quantity
$('select[name=qty]').change(function(){
var index = this.id.replace('qty','');
if($("#item_chk"+index).is(':checked')){
RemoveItem(index);
AddNewItem(index);
CalculateTotals();
}
});
// Years change, if someone changes the years
$('select[name=yrs]').change(function(){
var index = this.id.replace('yrs','');
if($("#item_chk"+index).is(':checked')){
RemoveItem(index);
AddNewItem(index);
CalculateTotals();
}
});
})
td {
text-align: left;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="Calculate.js"></script>
<body>
<div style="width: 100%; float:left; clear:both;" class="IlIaNd box">
<div id="item11" style="width:50%;">
<div style="width:50%; float:left;">
<input style="height:25px; width:25px;" type="checkbox" id="item_chk11" name="item_add" value="add" /> Adance Search HTML
</div>
<div style="width:50%; float:left;">
<div style="width:50%; float:left;">
<span style="font-size:12px;">Years: </span><select style="height:18px;" name="yrs" id="yrs11">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div style="width:50%; float:left;">
<span style="font-size:12px;">Quantity: </span><select name="qty" id="qty11" style='height:18px;'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="hidden" name="itemdescr" id="itemdescr11" value="Adance Search HTML">
<input type="hidden" name="servicefee" id="servicefee11" value="1000">
<input type="hidden" name="licensefee" id="licensefee11" value="3000">
<input type="hidden" name="traininghrs" id="traininghrs11" value="8">
</div>
<div style="width:100%; height:25px; clear:both; float:left;"></div>
</div>
<div id="item12" style="width:50%;">
<div style="width:50%; float:left;">
<input style="height:25px; width:25px;" type="checkbox" id="item_chk12" name="item_add" value="add" /> Adance Search PHP
</div>
<div style="width:50%; float:left;">
<div style="width:50%; float:left;">
<span style="font-size:12px;">Years: </span><select style="height:18px;" name="yrs" id="yrs12">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div style="width:50%; float:left;">
<span style="font-size:12px;">Quantity: </span><select name="qty" id="qty12" style='height:18px;'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="hidden" name="itemdescr" id="item_descr12" value="Adance Search PHP">
<input type="hidden" name="servicefee" id="servicefee12" value="1500">
<input type="hidden" name="licensefee" id="licensefee12" value="1000">
<input type="hidden" name="traininghrs" id="traininghrs12" value="2">
</div>
<div style="width:100%; height:25px; clear:both; float:left;"></div>
</div>
<div id="item14" style="width:50%;">
<div style="width:50%; float:left;">
<input style="height:25px; width:25px;" type="checkbox" id="item_chk14" name="item_add" value="add" /> Adance Search WWW
</div>
<div style="width:50%; float:left;">
<div style="width:50%; float:left;">
<span style="font-size:12px;">Years: </span><select style="height:18px;" name="yrs" id="yrs14">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div style="width:50%; float:left;">
<span style="font-size:12px;">Quantity: </span><select name="qty" id="qty14" style='height:18px;'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="hidden" name="itemdescr" id="item_descr14" value="Adance Search WWW">
<input type="hidden" name="servicefee" id="servicefee14" value="0">
<input type="hidden" name="licensefee" id="licensefee14" value="250">
<input type="hidden" name="traininghrs" id="traininghrs14" value="0">
</div>
<div style="width:100%; height:25px; clear:both; float:left;"></div>
</div>
<div id="item15" style="width:50%;">
<div style="width:50%; float:left;">
<input style="height:25px; width:25px;" type="checkbox" id="item_chk15" name="item_add" value="add" /> Adance Search HTTP
</div>
<div style="width:50%; float:left;">
<div style="width:50%; float:left;">
<span style="font-size:12px;">Years: </span><select style="height:18px;" name="yrs" id="yrs15">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div style="width:50%; float:left;">
<span style="font-size:12px;">Quantity: </span><select name="qty" id="qty15" style='height:18px;'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="hidden" name="itemdescr" id="item_descr15" value="Adance Search HTTP" >
<input type="hidden" name="servicefee" id="servicefee15" value="1000">
<input type="hidden" name="licensefee" id="licensefee15" value="0">
<input type="hidden" name="traininghrs" id="traininghrs15" value="10">
</div>
<div style="width:100%; height:25px; clear:both; float:left;"></div>
</div>
</div>
<center>
<table id="add_item_here" style="width:98%;" border="1">
<tbody>
<tr><th>Item</th><th>Years</th><th>Quantity</th><th>Training Hours</th><th>Total Item Cost</th></tr>
</tbody>
</table>
</center>
<center>
<table id="add_totals_here" style="width:98%;" border="1">
<tbody>
<tr><th colspan="3">Training Hours</th><th>Total Cost</th></tr>
</tbody>
</table>
</center>
</body>
</html>

Ok... Finally got the total hours and cost working on my price quote calculator...
function CalculateTotals(){
var total_cost = 0;
var total_training = 0;
$('input:checkbox:checked').each(function(){
var index = this.id.replace('item_chk','');
var item_cost = 0;
// Calculating item cost for just that one checkbox
item_cost+=parseInt($('#servicefee'+index).val());
item_cost*=parseInt($('#yrs'+index).val());
item_cost+=parseInt($('#licensefee'+index).val());
item_cost*=parseInt($('#qty'+index).val());
var traininghrs = parseInt($('#traininghrs'+index).val());
total_cost +=item_cost;
total_training +=traininghrs;
});
if(total_cost > 0 || total_training > 0) {
$('#add_totals_here tr:last').children().remove();
$('#add_totals_here tr:last').after('<tr ><td colspan="3" style=\"width:66%;\">TOTALS:</td><td style=\"width:18%;\">' + total_training + '</td><td style=\"width:16%;\">$'+ total_cost + '</td></tr>');
$('#add_item_here').show();
$('#add_totals_here').show();
}
else{
$('table[id^=add_item_here]').hide();
$('table[id^=add_totals_here]').hide();
}
To get the complete answer, feel free to email me... But wanna shout out to mbadeveloper. He helped me out a lot and helped me understand jquery a bit more. Thanks mbadeveloper.

Related

Calculate total amount from different dropdowns selected value

I am trying to calculate the total value of the selected value. In one row I have three options one is to select room that is one only, second is to select any one option and the same with the third column. When I am selecting options, it shows the correct "total Amount". Also on clicking on add room one new row with same options appears and there is no limit to add rows. But now what I am trying to do is When someone select values from different rows, "Total Amount" must show the total selected values.
Suppose, I have selected 1 room, 1st value is 5000 and 2nd value is 4000, then "Total Amount" is showing 9000. When I added one more row, in this again I selected 1 room, the 1st value is 6000 and the 2nd value is 9000. Now the " Total Amount" will show 24000 and this will work continuously on adding rows and selecting values.
Here is my HTML :
<table style="width: 100%;">
<tr style=" color: white;">
<th class="col-md-4 text-center" style="padding: 10px;">No. of Rooms : </th>
<th class="col-md-4 text-center" style="padding: 10px;">Adult's : </th>
<th class="col-md-4 text-center" style="padding: 10px;">Extra : </th>
</tr>
</table>
<table id="dataTable" style="width: 100%;">
<tr>
<td >
<select name="links" class="form-control person-3" action="post" id="myselect" onChange="document.getElementById('rooms').innerHTML = this.value;">
<option value="0" selected="selected">--select--</option>
<option value="1">1</option>
</select>
</td>
<td >
<select name="keywords" class="form-control person-1" action="post" id="myselect" onChange="document.getElementById('adults').innerHTML = this.value;">
<option value="0" selected="selected">--select--</option>
<option value="5000">1</option>
<option value="8000">2</option>
</select>
</td>
<td >
<select name="violationtype" class="form-control person-2" action="post" id="myselect" onChange="document.getElementById('extra').innerHTML = this.value;">
<option value="0" selected="selected">No Beds</option>
<option value="4000">Adult</option>
<option value="3000">Child below 12</option>
<option value="2000">Child below 7</option>
</select>
</td>
</tr>
</table>
<input type="button" value="Add Rooms" onclick="addRow('dataTable')" class="btn btn-primary" style="margin-top: 15px;" /><br><br>
<div class="row">
<div class="col-md-6" style="color: white;font-size: 20px;text-align: right;">Total Amount :</div>
<div class="col-md-6" style="color: white;font-size: 20px;text-align: left;">
<span class="addition"></span>
</div>
</div><br>
Here is my script through which I am calculating the total amount:
var add_1 =0;
var add_2 = 0;
var mul_3 = 0;
function displayValues() {
add_1 = parseInt($('.person-1').val());
add_2 = parseInt($('.person-2').val());
mul_3 = parseInt($('.person-3').val());
$(".addition-1").text(add_1);
$(".addition-2").text(add_2);
$(".multiplication_3").text(mul_3);
$(".addition").text( mul_3 *( add_1 + add_2) );
}
$(document).ready(function(){
$(".person-1").change(displayValues);
$(".person-2").change(displayValues);
$(".person-3").change(displayValues);
});
Here is the code to add rooms:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch (newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
Incomplete Code............. and existing has so many errors..
Try to make it work out .but addRow('dataTable'); defination missing.
`https://jsfiddle.net/asimshahiddIT/756tb80f/6/`
It seems you have same ID "myselect" for all select tag. Please give them unique IDs otherwise they'll misbehave.
Then if you are getting values from the class name what will be the case when one more row is added with same class names. Your logic will not work. Please try getting values with dynamic Ids provided to the select tag in each row.
`https://jsfiddle.net/asimshahiddIT/756tb80f/24/`
Please check the above link
Is this what you expected output
does this help?
$(document).ready(function(){
var dd1 =0;
var dd2 =0;
var dd3 =0;
var totalBillAmt = 0;
$("#myselect1").change(function(){ dd1 = Number($(this).val()); updateTotalPrice(); });
$("#myselect2").change(function(){ dd2 = Number($(this).val()); updateTotalPrice(); });
$("#myselect3").change(function(){ dd3 = Number($(this).val()); updateTotalPrice(); });
updateTotalPrice = function (){
totalBillAmt = dd1 + dd2 + dd3;
$("#totalPrice").text("total amount is:" + totalBillAmt);
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width: 100%;">
<tr style=" color: white;">
<th class="col-md-4 text-center" style="padding: 10px;">No. of Rooms : </th>
<th class="col-md-4 text-center" style="padding: 10px;">Adult's : </th>
<th class="col-md-4 text-center" style="padding: 10px;">Extra : </th>
</tr>
</table>
<table id="dataTable" style="width: 100%;">
<tr>
<td >
<select name="links" class="form-control person-3" action="post" id="myselect1" >
<option value="0" >--select--</option>
<option value="100">test option 1</option>
<option value="500">test option 2</option>
</select>
</td>
<td >
<select name="keywords" class="form-control person-1" action="post" id="myselect2" >
<option value="0" >--select--</option>
<option value="5000">another opt 1</option>
<option value="8000">another opt 2</option>
</select>
</td>
<td >
<select name="violationtype" class="form-control person-2" action="post" id="myselect3" >
<option value="0" >No Beds</option>
<option value="4000">Adult</option>
<option value="3000">Child below 12</option>
<option value="2000">Child below 7</option>
</select>
</td>
</tr>
</table>
<input type="button" value="Add Rooms" onclick="updateTotalPrice()" class="btn btn-primary" style="margin-top: 15px;" /><br><br>
<div id='totalPrice'></div>
<div class="row">
<div class="col-md-6" style="color: white;font-size: 20px;text-align: right;">Total Amount :</div>
<div class="col-md-6" style="color: white;font-size: 20px;text-align: left;">
<span class="addition"></span>
</div>
</div><br>

Invert names of IDs in a form using Javascript

I have the following form:
<form id="currency" name="form" onsubmit="return redirect(this)">
<div style="clear:both;text-align: center;">
<select id="firstcurrency" class="selex" name="firstcurrency" style="float: left;">
<option value="usd">USD</option>
<option value="btc">BTC</option>
</select>
<span style="margin: 0 auto;">=</span>
<select id="secondcurrency" class="selex" name="secondcurrency" style="float: right;">
<option value="btc">RRR</option>
<option value="xvg">XVG</option>
<option value="xrp">XRP</option>
<option value="ntx">NTX</option>
</select>
<p style="clear: both;"></p>
<p><input class="button" type="submit" value="Redirect Me" ></p>
<img src="/images/invert.png">Invert select
</form>
When I click on a tag (outside the form), I want it to do two things:
1) invert the values of IDs and names in the 2 select in the form above 2) change float values from left to right in the first select and from right to left in the second select.
After the click on a tag, the form should become:
<form id="currency" name="form" onsubmit="return redirect(this)">
<div style="clear:both;text-align: center;">
<select id="secondcurrency" class="selex1" name="secondcurrency" style="float: right;">
<option value="usd">USD</option>
<option value="btc">BTC</option>
</select>
<span style="margin: 0 auto;">=</span>
<select id="firstcurrency" class="selex2" name="firstcurrency" style="float: left;">
<option value="btc">RRR</option>
<option value="xvg">XVG</option>
<option value="xrp">XRP</option>
<option value="ntx">NTX</option>
</select>
<p style="clear: both;"></p>
<p><input class="button" type="submit" value="Redirect Me" ></p>
<img src="/images/invert.png">Invert select
</form>
It should do it again each time I press a tag.
What Javascript code would you use to do that?
Add separate css for the currencies then use this :
$('#invert').click(function(){
var firstCurr = $('#firstcurrency'),
secondCurr = $('#secondcurrency'),
tmpID = secondCurr.attr('id');
secondCurr.attr('id',firstCurr.attr('id'));
secondCurr.attr('name',firstCurr.attr('id'));
firstCurr.attr('id',tmpID);
firstCurr.attr('name',tmpID);
});
#secondcurrency {
float:right;
}
#firstcurrency {
float:left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="currency" name="form" onsubmit="return redirect(this)">
<div style="clear:both;text-align: center;">
<select id="firstcurrency" class="selex" name="firstcurrency">
<option value="usd">USD</option>
<option value="btc">BTC</option>
</select>
<span style="margin: 0 auto;">=</span>
<select id="secondcurrency" class="selex" name="secondcurrency">
<option value="btc">RRR</option>
<option value="xvg">XVG</option>
<option value="xrp">XRP</option>
<option value="ntx">NTX</option>
</select>
<p style="clear: both;"></p>
</div>
<p><input class="button" type="submit" value="Redirect Me" ></p>
<a id="invert" href="javascript:void(0)"><!--href="convertitore-home"<img src="/images/invert.png">-->Invert select</a>

Html/ javascript form calculation issues

I'm new to coding for websites and am really struggling! I'm trying to add the select values from different select menu options to a total which will then give me a figure I can add to a number entered in a free text box.
I"ve searched extensively on the net and on this site and have found examples that are similar but when I've tried them out it doesn't work with my additions/changes!
Any help would be greatly appreciated!!!! Below is my html and also javascript.
Thanks in advance!
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="example.js"></script>
</head>
<form action="" id="myform" onsubmit="return false;">
<div class="cont_order">
<div data-role="fieldcontain">
<label for="selectmenu" class="select">
<div align="right"></div>
</label>
<div align="center"></div>
</div>
<div align="center">
</p>
<select name="Sport" id="Sport" data-native-menu="false" data-theme="a" onchange="calculateTotal()">
<option value="0">Sport</option>
<option value="Tennis">Tennis</option>
<option value="Golf">Golf</option>
<option value="Soccer">Soccer</option>
<option value="Rugby">Rugby</option>
<option value="Polo">Polo</option>
<option value="Fencing">Fencing</option>
<option value="Swimming">Swimming</option>
</select>
<br/>
<label for="selectmenu" class="select">
<div align="right"></div>
</label>
<div align="center"></div>
</div>
<div align="center">
</p>
<select name="Star Sign" id="SS" data-native-menu="false" onchange="calculateTotal()">
<option value="0">Star Sign</option>
<option value="1.5">Aries</option>
<option value="3">Taurus</option>
<option value="1.5">Gemini</option>
<option value="2">Cancer</option>
<option value="3">Leo</option>
<option value="1.5">Virgo</option>
<option value="2">Libra</option>
<option value="3">Scorpio</option>
<option value="2">Sagittarius</option>
<option value="1.5">Capricorn</option>
<option value="0.5">Aquarius</option>
<option value="0.5">Pisces</option>
</select>
<br/>
<label for="selectmenu" class="select">
<div align="right"></div>
</label>
<div align="center"></div>
</div>
<div align="center">
</p>
<br/>
<select name="Hobby" id="Hobby" data-native-menu="false" onchange="calculateTotal()">Hobby
<option value="0">Hobby</option>
<option value="A lot">A lot</option>
<option value="Some">Some</option>
<option value="Few">Few</option>
<option value="None">None</option
</select>
</div>
<br/>
<label for="selectmenu" class="select">
<div align="right"></div>
</label>
<div align="center"></div>
</div>
<div align="center">
</p>
<br/>
<select name="Favourite Colour" id="FC" data-native-menu="false" onchange="calculateTotal()">
<option value="0">Favourite Colour</option>
<option value="1.5">Black</option>
<option value="3">Blue</option>
<option value="1.5">Brown</option>
<option value="2">Green</option>
<option value="3">Orange</option>
<option value="1.5">Pink</option>
<option value="2">Purple</option>
<option value="3">Red</option>
<option value="2">Yellow</option>
<option value="1.5">White</option>
<option value="4">Other</option>
</select>
<br/>
<p>
<br/>
<div data-role="fieldcontain">
<label for="Age">
<div align="center">Age
:
</div>
</label>
<div align="center">
<input type="number" name="Age" id="Age" value="" />
</div>
</div>
</form>
<p> </p>
<p> <a href="#" data-role="button"> <input type='submit' id='Submit' value='submit' onclick="calculateTotal()"/>
<br/>
</div>
</form>
</div>
</body>
</html>
// JavaScript Document
var Sport = new Array();
Sport["Tennis"]=1.5;
Sport["Golf"]=3;
Sport["Soccer"]=2;
Sport["Rugby"]=3;
Sport["Polo"]=1.5;
Sport["Fencing"]=2;
Sport["Swimming"]=3;
var SS = new Arr4ay();
Star_Sign["Aries"]=1.5;
Star_Sign["Taurus"]=3;
Star_Sign["Cancer"]=2;
Star_Sign["Leo"]=3;
Star_Sign["Virgo"]=1.5;
Star_Sign["Libra"]=2;
Star_Sign["Scorpio"]=3;
Star_Sign["Sagittarius"]=2;
Star_Sign["Capricorn"]=1.5;
Star_Sign["Aquarius"]=0.5;
Star_Sign["Pisces"]=1.5;
var Hobby = new Array();
Hobby["A lot"]=0.5;
Hobby["Some"]=1;
Hobby["Few"]=1;
Hobby["None"]=2;
var FC = new Array();
Favourite_Colour["Blue"]=3;
Favourite_Colour["Brown"]=1.5;
Favourite_Colour["Green"]=2;
Favourite_Colour["Orange"]=3;
Favourite_Colour["Pink"]=1.5;
Favourite_Colour["Purple"]=2;
Favourite_Colour["Red"]=3;
Favourite_Colour["Yellow"]=2;
Favourite_Colour["White"]=1.5;
Favourite_Colour["Other"]=4;
function calc() {
var Sport = Number(document.getElementsById("Sport").value);
var SS = Number(document.getElementsById("SS").value);
var Hobby = Number(document.getElementsById("Hobby").value);
var FC = Number(document.getElementsById("FC").value);
var Age = Number(document.getElementsById("Age").value);
var total = Sport + SS + Hobby + FC;
var Score = Age = Score
}// JavaScript Document
In your function calc replace Number with either parseInt if you always expect an integer from your value or parseFloat otherwise.
Unless I am misunderstanding what you're after you could just use the Array.reduce function:
Array.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
So for you:
function calc() {
new tempAry = [
Number(parseInt (document.getElementsById("Sport").value),10),
Number(parseInt (document.getElementsById("SS").value),10),
Number(parseInt (document.getElementsById("Hobby").value),10),
Number(parseInt (document.getElementsById("FC").value),10)
];
var total = tempAry.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
//No idea what these are for
//var Age = Number(document.getElementsById("Age").value);
//var Score = Age = Score
}
Not sure exactly what you're after, nor what `Score is meant for, but total should equal the total number.

IE showing two div instead of one

I made a button which is showing and hiding div on click by using jQuery. But IE showing another extra div and rest of the browsers showing it properly.
<div class="hide-search-button">
<div class="l-f-w" id="locate">
<h1> Location </h1>
<input type="hidden" value="" id="loc_hidden"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h1>State</h1></td>
<td> <select onChange="selectCity(this.options[this.selectedIndex].value)" name="state_name" id="state_name">
<option value="Choose">Choose</option>
<?php
//connect with database
$sqlCountry="select DISTINCT state from sheet1
order by state asc ";
$resCountry=mysql_query($sqlCountry);
$checkCountry=mysql_num_rows($resCountry);
// fetch all country
while($rowCountry=mysql_fetch_array($resCountry)){
?>
<option value="<?php echo $rowCountry['state']?>">
<?php echo $rowCountry['state']?>
</option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td><h1>Region</h1></td>
<td><select id="region_name" name="region_name"
onchange="selectState(this.options[this.selectedIndex].value)">
<option value="Choose">Choose</option>
</select>
</td>
</tr>
<!--<tr>
<td><h1>Suburb</h1></td>
<td><select id="suburb_name" name="suburb_name">
<option value="Choose">Choose</option>
</select></td>
</tr>-->
<tr>
<td><h1>Pin Code</h1></td>
<td> <input type="text"value=" " name="pin" id="pin"></td>
</tr>
</table>
<input type="hidden" value="2" name="searchform" />
<script>
function span_go()
{
//var locate = document.getElementById("menuSelection").innerHTML;
var service = document.getElementById("sertype").innerHTML;
//document.getElementById("locatetext").value = locate;
document.getElementById("servicetext").value = service;
//alert (service);
}
</script>
<input type="hidden" value="" id="servicetext" name="servicetext"/>
<?php /*?><a tabindex="0" href="menuContent.html" class="fg-button fg-button-icon-right ui-widget ui-state-default ui-corner-all" id="flyout"><div class="arrow_wrap"><span class="ui-icon ui-icon-triangle-1-s"></span> </div> <span id="menuSelection"> <?php echo $location ?></span></a><?php */?>
</div> <!-- location field wrapper ends here -->
<div class="service_type_wrap s-t-w">
<h1> Service Type </h1>
<select multiple="multiple" label="choose" style="margin-left:8px;" name="tex_value" id="worktype">
<option value="Long Day Care">Long Day Care</option>
<option value="Occasional Care">Occasional Care</option>
<option value="In Home Care">In Home Care</option>
<option value="Before School Care">Before School Care</option>
<option value="After School Care">After School Care</option>
<option value="Vacation">Vacation Care</option>
<option value="Special Care">Special Care</option>
<option value="Permanent / Contract">Permanent / Contract</option>
<option value="Part time">Part time</option>
</select>
</div> <!-- service type wrapper ends here -->
<div class="f-r-w" style="margin-left:10px;">
<h1> Filter Results </h1>
<select data-placeholder="<?php echo $sorting; ?>" class="chzn-select" style="width:200px;" tabindex="2"name="q_state" id="jobsort">
<option value="title">Sort by job title</option>
<option value="date">Sort by date</option>
</select>
</div> <!-- filter results wrapper ends here -->
<div class="go_button_wrap">
<input name="submit" type="submit" value="Go" id="custum_button" onclick="span_go();">
</div> <!-- go button ends here -->
</form>
</div>
JS:
$(document).ready(function() {
$("#button").toggle(function() {
//$(this).text('Hide Advance Search');
}, function() {
//$(this).text('Show Advance Search');
}).click(function(){
$(".hide-search-button").slideToggle("slow");
event.preventDefault();
});
});
Screenshot shown below:
Try to change click part of your code. You need to pass event object into the handler. In IE actually there is window.event object. Maybe it causes the issue:
.click(function(event) {
$(".hide-search-button").slideToggle("slow");
event.preventDefault();
});

Form button that populates textbox is not working in Internet Explorer

My button OnClick event uses Javascript to populate two wide textboxes with the selected options from multiple select boxes.
Everything works perfect in Firefox and Chrome but nothing happens when you click the button in Internet Explorer. I would appreciate any help.
<!DOCTYPE html>
<html>
<script type="text/javascript">
function addbutton1() {
var box1 = document.getElementById('dropdown1').options[document.getElementById('dropdown1').selectedIndex].text;
var box2 = document.getElementById('dropdown2').options[document.getElementById('dropdown2').selectedIndex].text;
var box3 = document.getElementById('dropdown3').options[document.getElementById('dropdown3').selectedIndex].text;
var box4 = document.getElementById('dropdown4').options[document.getElementById('dropdown4').selectedIndex].text;
var box5 = document.getElementById('dropdown5').options[document.getElementById('dropdown5').selectedIndex].text;
var box6 = document.getElementById('dropdown6').options[document.getElementById('dropdown6').selectedIndex].text;
textbox1.value = box1 + ', ' + box2 + ', ' + box3 + ', ' + box4;
textbox2.value = box5 + ', ' + box6;
}
</script>
<body>
<form name="customform" id="wstForm_Custom" action="%wstx.formmailerurl%" method="post" labelID="formLabel_CustomForm">
<style>
#wstForm_Custom {background-color:transparent;} .specialEmailField {display: none;visibility:hidden;height:0px;} .emailDisplay {display: none;visibility:hidden;height:0px;}
</style>
<table>
<tr><td style="font-family:Arial; text-align: left; font-size: 10pt;">Row 1:</td>
<td><select style="width: 150px" id="dropdown1">
<option value='0'>Select</option>
<option value='100.00'>Option 1a</option>
<option value='100.00'>Option 1b</option>
</select></td>
<td><select style="width: 150px" id="dropdown2">
<option>Select</option>
<option>Option 2a</option>
<option>Option 2b</option>
</select></td></tr>
<tr><td style="font-family:Arial; text-align: left; font-size: 10pt;">Row 2:</td>
<td><select style="width: 150px" id="dropdown3">
<option>Select</option>
<option>option 3a</option>
<option>Option 3b</option>
</select></td>
<td><select style="width: 150px" id="dropdown4">
<option>Select</option>
<option>Option 4a</option>
<option>Option 4b</option>
</select></td>
<tr><td style="font-family:Arial; text-align: left; font-size: 10pt;">Row 3:</td>
<td><select style="width: 150px" id="dropdown5">
<option value='0'>Select</option>
<option value='0'>Option 5a</option>
<option value='0'>Option 5b</option>
</select></td></tr>
<tr><td style="font-family:Arial; text-align: left; font-size: 10pt;">Row 4:</td>
<td><select style="width: 150px" id="dropdown6">
<option value='0'>Select</option>
<option value='100.00'>Option 6a</option>
<option value='100.00'>Option 6b</option>
</select></td>
</table>
<button type="button" onclick="addbutton1(this.form)">Add Options to Textboxes</button>
<table style="width: 700px">
<tr>
<input style="width:480px" name="First Textbox" id="textbox1" type="text" AUTOCOMPLETE="OFF"/></td>
</tr>
<tr>
<input style="width:480px" name="Second Textbox" id="textbox2" type="text" AUTOCOMPLETE="OFF"/></td>
</tr>
</table>
<p style="width: 740px; text-align: center">
<input style="width:100px;" title="Submit" id="wstForm_Custom_Submit" onclick="return wstxSubmitForm(this);" type="submit" value="Submit"/>
<input style="width:50px;" title="Reset Form" id="wstForm_Custom_Reset" type="reset" value="Reset"/>
</p>
</form>
</body>
You should use document.getElementById('textbox1').value not simple textbox1.value.
Here is an example: http://jsfiddle.net/WbTmf/1/

Categories