I am new to the development field.
There are two problem with this code.
First when I click button for add_Row then a row gets added on screen but it disappears after 1-2 seconds and same thing happens with group_Create() popup.
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Inventory Expert</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="../../Bootstrap-3.3.2-dist/css/bootstrap.min.css"/>
<link rel="stylesheet" href="../../CSS/ProductMaster/ProductMaster.css"/>
<link rel="stylesheet" href="../../CSS/ProductMaster/RawMaterialGroup.css"/>
<script src="../../JAVASCRIPT/ProductMaster/ProductMaster.js"></script>
<script src="../../JAVASCRIPT/ProductMaster/RawMaterialGroup.js"></script>
<script src="../../jquery-1.11.2.js"></script>
<script src="../../jquery-ui-1.11.2.custom/jquery-ui.js"></script>
<script src="../../jquery-ui-1.11.2.custom/jquery-ui.min.js"></script>
</head>
<body>
<img id="top" src="../../Images/shaddow_line_top.png" alt=""/>
<div class="container">
<h1><a>Product Master</a></h1>
<form id="productmasterForm">
<table class="table" id="productmasterTable">
<tr>
<td class="W45">Product Code <input id="productid" type="text"/></td>
<td class="W45">Product Name <input id="productname" type="text"/></td>
<td class="W10"></td>
</tr>
<tr>
<td class="W45">Basic Raw Material <input id="basicraw" type="text"/></td>
<td class="W45">Group Name <input id="groupname" type="text"/></td>
<td class="W10">
<div class="btn-group">
<button class="btn btn-info btn-sm" id="groupcreate" onclick="group_Create()">C</button>
<button class="btn btn-info btn-sm" id="groupedit" onclick="group_Edit()">E</button>
</div>
</td>
</tr>
<tr>
<td class="W40">Raw Material <input id="rm1" type="text"/></td>
<td class="W30">Size <input id="s1" type="text"/></td>
<td class="W20">Qty. <input id="q1" type="text"/></td>
<td class="W10">
<div class="btn-group">
<button class="btn btn-info btn-sm" onclick="maprawNsize()">C</button>
<button class="btn btn-info btn-sm" onclick="maprawNsize_Edit()">E</button>
<button class="btn btn-info btn-sm" id="pma" onclick="add_Row()">A</button>
</div>
</td>
</tr>
<tr>
<td class="W45">VAT Rate <input id="vat" type="text"/></td>
<td class="W45">Unit Of Measure <input id="uom" type="text"/></td>
<td class="W10"></td>
</tr>
<tr>
<td class="W45">Manufacturing Cost <input id="menucost" type="text"/></td>
<td class="W45">Sale Rate <input id="salerate" type="text"/></td>
<td class="W10"></td>
</tr>
<tr>
<td class="W45">Maximum Retail Price <input id="mrp" type="text"/></td>
<td class="W45">Default Discount <input id="defdisc" type="text"/></td>
<td class="W10"></td>
</tr>
<tr>
<td class="W45">Rate List Date <input id="listdate" type="text"/></td>
<td class="W45">Kit Reference <input id="kitref" type="text"/></td>
<td class="W10"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
JavaScript File ProductMaster.js
var rowCount = 1;
var rowPosition = 3;
var id = 2;
function add_Row() {
if (rowCount < 11) {
var table = document.getElementById("productmasterTable");
var row = table.insertRow(rowPosition);
rowPosition++;
for (i = 0; i < 1; i++) {
var td1 = document.createElement("td");
td1.innerHTML = 'Raw Material <input id="rm' + id + '" type="text"/>';
row.appendChild(td1);
td1.setAttribute("class", "W40");
}
for (i = 0; i < 1; i++) {
var td2 = document.createElement("td");
td2.innerHTML = 'Size <input id="s' + id + '" type="text"/>';
row.appendChild(td2);
td2.setAttribute("class", "W30");
}
for (i = 0; i < 1; i++) {
var td3 = document.createElement("td");
td3.innerHTML = 'Qty. <input id="q' + id + '" type="text"/>';
row.appendChild(td3);
td3.setAttribute("class", "W20");
}
id++;
rowCount++;
}
else {
alert("Only 10 Allowed");
}
}
JavaScript File RawMaterialGroup.js
function group_Create(){
document.getElementById('rawgroup').style.display = "block";
}
function group_Hide(){
document.getElementById('rawgroup').style.display = "none";
}
function group_Edit(){
alert("I Am Clicked");
}
var rowCount = 5;
var rowPosition = 7;
var id = 2;
function add_rawMaterial() {
if (rowCount < 16) {
var table = document.getElementById("groupTable");
var row = table.insertRow(rowPosition);
rowPosition++;
for (i = 0; i < 1; i++) {
var td1 = document.createElement("td");
td1.innerHTML = 'Raw Material <input id="rmgrm' + id + '" type="text"/>';
row.appendChild(td1);
td1.setAttribute("class", "W40");
}
for (i = 0; i < 1; i++) {
var td2 = document.createElement("td");
td2.innerHTML = 'Qty. <input id="rmgq' + id + '" type="text"/>';
row.appendChild(td2);
td2.setAttribute("class", "W20");
}
for (i = 0; i < 1; i++) {
var td3 = document.createElement("td");
td3.innerHTML = 'UOM <input id="rmguom' + id + '" type="text"/>';
row.appendChild(td3);
td3.setAttribute("class", "W20");
}
id++;
rowCount++;
}
else {
alert("Only 15 Allowed");
}
}
CSS File ProductMaster.css
body{
text-align: center;
overflow: hidden;
}
td{
float: left;
text-align: left
}
#rm1,#rm2,#rm3,#rm4,#rm5,#rm6,#rm7,#rm8,#rm9,#rm10{
width: 250px;
height: 30px
}
#s1,#s2,#s3,#s4,#s5,#s6,#s7,#s8,#s9,#s10{
width: 250px;
height: 30px;
}
#q1,#q2,#q3,#q4,#q5,#q6,#q7,#q8,#q9,#q10{
width: 100px;
height: 30px;
}
.W45{
width: 45%;
}
.W10{
width: 10%;
}
.W15{
width: 15%;
}
.W20{
width: 20%;
}
.W30{
width: 30%;
}
#productid{
width: 100px;
height: 30px;
}
#productname{
width: 300px;
height: 30px;
}
#basicraw{
width: 259px;
height: 30px;
}
#groupname{
width: 315px;
height: 30px;
}
#productmasterForm{
font-size: 20px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#vat{
width: 80px;
height: 30px;
}
#uom{
width: 80px;
height: 30px;
}
#menucost{
width: 80px;
height: 30px;
}
#salerate{
width: 80px;
height: 30px;
}
#mrp{
width: 80px;
height: 30px;
}
#defdisc{
width: 80px;
height: 30px;
}
#listdate{
width: 120px;
height: 30px;
}
#kitref{
width: 100px;
height: 30px;
}
CSS File RawMaterialGroup.css
h2 {
background-color:#00a2e2;
padding:20px 20px;
margin:-10px -10px;
text-align:center;
border-radius:10px 10px 0 0;
border: 1px solid #313131;
}
#rawgroup{
width: 100%;
height: 100%;
opacity: 0.95;
top: 0;
bottom: 0;
display: none;
position: fixed;
background-color: #313131;
overflow: hidden;
alignment-adjust: central;
}
div#groupPopup{
position: fixed;
left: 18%;
top: 17%;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
img#close_group{
position: absolute;
right: -7px;
top: -7px;
cursor: pointer;
}
#groupForm{
max-width: 900px;
min-width: 900px;
padding: 10px 10px;
border: 2px solid gray;
border-radius: 10px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
background-color: #fff;
margin:-10px -11px;
text-align:center;
border-radius:10px 10px 10px 10px;
}
.W40{
width: 40%;
}
.W20{
width: 20%;
}
.W10{
width: 10%;
}
#rmgrm1,#rmgrm2,#rmgrm3,#rmgrm4,#rmgrm5,#rmgrm6,#rmgrm7,#rmgrm8,#rmgrm9,#rmgrm10,#rmgrm11,#rmgrm12,#rmgrm13,#rmgrm14,#rmgrm15{
width: 215px;
height: 30px;
}
#rmgq1,#rmgq2,#rmgq3,#rmgq4,#rmgq5,#rmgq6,#rmgq7,#rmgq8,#rmgq9,#rmgq10,#rmgq11,#rmgq12,#rmgq13,#rmgq14,#rmgq15{
width: 80px;
height: 30px;
}
#rmguom1,#rmguom2,#rmguom3,#rmguom4,#rmguom5,#rmguom6,#rmguom7,#rmguom8,#rmguom9,#rmguom10,#rmguom11,#rmguom12,#rmguom13,#rmguom14,#rmguom15{
width: 50px;
height: 30px;
}
#rmggn{
width: 200px;
height: 30px;
}
#rmggi{
width: 100px;
height: 30px;
}
You need to add type="button" since the default action in some browsers is a submit
<button type="button" ....>C</button>
<button type="button" ....>E</button>
Alternatively add ;return false to your onclick events like this:
onclick="add_Row(); return false"
to not submit the page
Better would be to assign the event handlers in an onload event but still you need to preventDefault the button's click event if it is not type="button"
Related
I want a table (table1) to be displayed n times according the number entered by the user in the input form. Then for each instance of this table the user should be able to click on each row and another relevant table should be displayed.
So far I can produce table1 n times according to user input. But then the function myFunction_disease is only applying to the first iteration of table1. I want each copy of table1 to controlled independently depending on what the user clicks.
Here is jsfiddle: https://jsfiddle.net/k0x4y6d2/1/
<div class="user_input_outer" id="num_of_conditions">
<div class= "content_text"> Number of conditions:</div>
<div class="user_input">
<input type="number" id="numb_conditions" onchange="myFunction_num_conditions();" min="2" max="1"> </div>
</div>
<div id=condition_detail>
</div>
function myFunction_num_conditions(x) {
container = document.getElementById('numb_conditions')
var number = container.value
for (var i = 0; i < number; i++) {
count = i+1
document.getElementById("condition_detail").innerHTML += 'Condition' + (i + 1);
let newdiv = document.createElement('div');
newdiv.innerHTML = "<table id=table1 class=table> <tr onclick='myFunction_disease(this)'><td>cardiac </td></tr> <tr onclick='myFunction_disease(this)'><td> respiratory </td></tr> <tr onclick='myFunction_disease(this)'><td>infection</td></tr></table><table id=table2 class=table> <tr onclick='myFunction_cardiac(this)'><td>MI </td></tr> <tr onclick='myFunction_cardiac(this)'> <td> valve disease </td></tr> <tr 'myFunction_cardiac(this)'><td>CCF</td></tr></table> <table id=table3 class=table> <tr onclick='myFunction_respiratory(this)'><td>COPD </td></tr> <tr onclick='myFunction_respiratory(this)'><td> asthma </td></tr> <tr onclick='myFunction_respiratory(this)'><td>bronchiectasis</td></tr></table><table id=table4 class=table> <tr onclick='myFunction_infectious(this)'><td>TB </td></tr> <tr onclick='myFunction_infectious(this)'><td> pneumonia </td></tr> <tr onclick='myFunction_infectious(this)'><td>cellulitis</td></tr></table>"
document.getElementById('condition_detail').appendChild(newdiv);
}
}
function myFunction_disease(x) {
const disease = document.querySelector("#table1");
const cardiac = document.querySelector("#table2");
const respiratory = document.querySelector("#table3");
const infectious = document.querySelector("#table4");
if (x.rowIndex== 0){
cardiac.style.display= "table"
respiratory.style.display= "none"
infectious.style.display = "none"
}
else if (x.rowIndex== 1){
cardiac.style.display= "none"
respiratory.style.display= "table"
infectious.style.display = "none"
}
else if (x.rowIndex== 2){
cardiac.style.display= "none"
respiratory.style.display= "none"
infectious.style.display = "table"
}
}
div.user_input {
display: table-cell;
width: 150px;
font-family: sans-serif;
font-size: 12;
line-height: 10px;
}
div.user_input > input {
width: 140px;
margin: 0px;
padding: 0px;
font-family: sans-serif;
font-size: 14;
line-height: 19px;
}
.user_input:focus {
outline: 0 !important;
}
.table {
margin: 25px 0;
font-size: 13;
font-family: sans-serif;
min-width: 200px;
margin-left: auto;
margin-right: auto;
width: 25%;
border-style: solid;
border-width: 1px;
}
.table td{
margin: 25px 0;
font-size: 13;
font-family: sans-serif;
min-width: 200px;
margin-left: auto;
margin-right: auto;
width: 25%;
border-style: solid;
border-width: 1px;
}
#table2 {
display: none;
}
#table3 {
display: none;
}
#table4 {
display: none;
}
That's what you need. For adding more buttons and tables you can just simply add a new table and give a new class table$ (where $ is number, example: table4) and add a new button with data-table-show=$ attribute. That's all😉.
// Creating conditions
const numInput = document.getElementById('num'),
conditionForm = document.getElementById('condition-form'),
tablesContainer = document.getElementById('tables'),
conditionText = tablesContainer.innerHTML;
conditionForm.onsubmit = async function(e) {
e.preventDefault();
const numValue = Number(numInput.value);
await (tablesContainer.innerHTML += conditionText.repeat(numValue));
addEventListenersToButtons();
}
// Working with table filtering
function addEventListenersToButtons() {
let buttons = document.querySelectorAll('#tables tr.btn');
buttons.forEach(function(button, i) {
button.onclick = function() {
const tables = button.parentNode.parentNode.parentNode.querySelectorAll('.data-table');
tables.forEach(table => {
if (table.classList.contains(`table${button.dataset.tableShow}`)) {
table.classList.remove('hide')
} else {
table.classList.add('hide')
}
});
}
})
}
addEventListenersToButtons();
.tables-group table {
margin: 25px 0;
font-size: 13;
font-family: sans-serif;
min-width: 200px;
margin-left: auto;
margin-right: auto;
width: 25%;
border-style: solid;
border-width: 1px;
}
.tables-group table td {
margin: 25px 0;
font-size: 13;
font-family: sans-serif;
min-width: 200px;
margin-left: auto;
margin-right: auto;
width: 25%;
border-style: solid;
border-width: 1px;
}
.data-table.hide {
display: none;
}
<div class="container">
<form id="condition-form">
<label for="num">Number of conditions</label>
<input type="number" name="conditions" id="num" min="0">
<button type="submit" name="conditions">Submit</button>
</form>
<hr>
<div id="tables">
<div class="condition">
<div class="tables-group">
<table>
<tr class="btn cardiac" data-table-show="1">
<td>cardiac</td>
</tr>
<tr class="btn respiratory" data-table-show="2">
<td>respiratory</td>
</tr>
<tr class="btn infection" data-table-show="3">
<td>infection</td>
</tr>
</table>
<table class="data-table table1 hide">
<tr>
<td>MI</td>
</tr>
<tr>
<td>valve disease</td>
</tr>
<tr 'myFunction_cardiac(this)'>
<td>CCF</td>
</tr>
</table>
<table class="data-table table2 hide">
<tr>
<td>COPD</td>
</tr>
<tr>
<td>asthma</td>
</tr>
<tr>
<td>bronchiectasis</td>
</tr>
</table>
<table class="data-table table3 hide">
<tr>
<td>TB</td>
</tr>
<tr>
<td>pneumonia</td>
</tr>
<tr>
<td>cellulitis</td>
</tr>
</table>
</div>
</div>
</div>
</div>
What I made over here is a calculator with two display , some operators and number buttons, obviously in HTML.
But , I cannot figure out how to implement the decimal seperator.
At first , I thought that I could use maybe a variable as a boolean switch , but turns out , the number() function destroys the . decimal seperator before I can reset it to true.
Below is a sample of the html as well as the GUI.
I was thinking of making a new variable called var isExist, which can scan if the decimal still exists or not and then work out.
But anyways , code is given below.
Did not use JSFiddle , because , it seems that similar services don't work in my region
enter image description here
<html>
<head>
<link rel="stylesheet" href="css/index.css">
<style type="text/css">
#font-face{
font-family: josefin-sans light;
src: url(font/static/JosefinSans-Light.ttf);
}
</style>
</head>
<body>
<div id="mySidenav" class="sidenav">
×
About
Services
Clients
Contact
</div>
<div id="swipe-area"></div>
<span id= "menu" onclick="openNav()"> ☰</span>
<span id="title">Calculator</span>
<div id="calc">
<table>
<tr rowspan="2" id="display">
<th colspan="4">
<p id="prev"></p>
<p id="curr"></p>
</th>
</tr>
<tr class="keyboard">
<th><input type="button" class="operator" id="clear" value="C"></th>
<th><input type="button" class="operator" id="backspace" value=←></th>
<th><input type="button" class="operator" id="invert" value=±></th>
<th><input type="button" class="operator" id="/" value=÷></th>
</tr>
<tr class="keyboard">
<th><input type="button" class="number" id="7" value="7"></th>
<th><input type="button" class="number" id="8" value="8"></th>
<th><input type="button" class="number" id="9" value="9"></th>
<th><input type="button" class="operator" id="*" value=×></th>
</tr>
<tr class="keyboard">
<th><input type="button" class="number" id="4" value="4"></th>
<th><input type="button" class="number" id="5" value="5"></th>
<th><input type="button" class="number" id="6" value="6"></th>
<th><input type="button" class="operator" id="-" value=−></th>
</tr>
<tr class="keyboard">
<th><input type="button" class="number" id="1" value="1"></th>
<th><input type="button" class="number" id="2" value="2"></th>
<th><input type="button" class="number" id="3" value="3"></th>
<th><input type="button" class="operator" id="+" value=+></th>
</tr>
<tr class="keyboard">
<th></th>
<th><input type="button" class="number" id="0" value="0"></th>
<th><input type="button" class="operator" id="." value=˙></th>
<th><input type="button" class="operator" id="=" value==></th>
</tr>
</table>
</div>
</body>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/jquery.touchSwipe.min.js"></script>
<script type="text/javascript" src="js/swipe.js"></script>
</html>
function openNav(){//ignore this
document.getElementById("mySidenav").style.width ="100%";
}
function closeNav(){//ignore this too
document.getElementById("mySidenav").style.width ="0px";
}
function getPrevious(){//gets the value for top display
return document.getElementById("prev").innerText;
}
function setPrevious(num){//sets the value for top display
document.getElementById("prev").innerText=num;
}
function getCurrent(){//gets the value for bottom display
return document.getElementById("curr").innerText;
}
function setCurrent(num){//sets the value for bottom display
if(num==""){
document.getElementById("curr").innerText=num;
}
else{
document.getElementById("curr").innerText=getFormattedNumber(num)+symbol;
symbol="";
}
}
function getFormattedNumber(num){//formats number into indian numeral system
var n = Number(num);
var value = n.toLocaleString("hi-IN");
return value;
}
function reverseNumber(num){//extract number from here
return Number(num.replace(/,/g,''));
}
//catch operation of operator from here
var operator = document.getElementsByClassName("operator");
var rep=true;//---->this is the switch to enable decimal button(not functional)
var del;
for(var i=0;i<operator.length;i++){
operator[i].addEventListener("click",function(){
if(this.id=="clear"){
setPrevious("");
setCurrent("");
}
else
if(this.id=="backspace"){
var output=reverseNumber(getCurrent()).toString();
if(output){
del=output.charAt(output.length-1);//I am sure the code went wrong here
if(del=="."){
rep=true;
}
output=output.substr(0,output.length-1);
setCurrent(output);
}
}
else
if(this.id=="invert"){
var output=reverseNumber(getCurrent());
if(output!=""){
output=-output;
setCurrent(output);
}
}
else
if(this.id=="."){//this is probably WHERE YOU WANT TO CHECK
var output=reverseNumber(getCurrent()).toString();
if(output!=""){
if(rep==true){
symbol=this.id;
output=output
setCurrent(output);
rep=false;
}
}
}
else{
var current=getCurrent();
var previous=getPrevious();
if(output=="" && history!=""){
if(isNaN(history[history.length-1])){
history=history.substr(0,history.length-1);
}
}
//code is incomplete here , ignore it
}
})
}
var number = document.getElementsByClassName("number");
for(var i=0;i<number.length;i++){
number[i].addEventListener("click",function(){
var output=reverseNumber(getCurrent());
if(output!=NaN){
if(getCurrent().charAt(getCurrent().length-1)=="."){
output=output+(0.1*this.id)
setCurrent(output);
}
else{
output=output+this.id;
setCurrent(output);
}
}
})
}
body{
margin: 0px;
background: linear-gradient(to left top,rgb(99, 99, 255),rgb(126, 197, 255));
font-family: josefin-sans light;
}
.sidenav{
height: 100%;
width: 0%;
position:fixed;
z-index: 1;
top:0;
left: 0;
background-color: rgb(255, 255, 255);
overflow-x: hidden;
padding-top: 60px;
transition: 0.5s;
}
.sidenav a{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: rgb(132, 132, 255);
display: block;
transition: 0.3s;
}
.sidenav a:hover{
color: #000000;
font-size: 230%;
}
.sidenav .closebtn{
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
.sidenav .closebtn:hover{
color: red;
}
#menu,#title{
font-size: 250%;
color: white;
}
#title{
position: absolute;
left: 50%;
top: 3%;
z-index: 0;
transform: translate(-50%,-50%);
}
#main{
transition: margin-left .5s;
padding: 20px;
}
#media screen and (max-height:450px){
.sidenav{ padding-top: 15px;}
.sidenav a{font-size: 18px;}
}
::-webkit-scrollbar{
height: 5px;
}
::-webkit-scrollbar-thumb{
background: rgb(223, 223, 223);
border-radius: 10px;
}
table{
margin: auto;
text-align: center;
border-spacing: 0 15px;
padding: 10px;
table-layout: fixed;
}
input[type=button]{
border: none;
outline: none;
border-radius: 100%;
height: 100%;
width: 100%;
font-weight: 500;
font-size:large;
}
#calc{
background-color:rgba(240, 248, 255, 0.4);
}
#display{
background-color: rgba(0, 0, 0, 0.4);
}
#prev,#curr{
max-width: 326px;
overflow: auto;
}
#prev{
text-align: right;
font-size: 150%;
margin: 0px 0px 0px 0px;
}
#curr{
text-align: right;
font-size: 400%;
margin: 0px 0px 0px 0px;
}
#display{
height: 150px;
}
tr.keyboard th{
min-width: 80px;
height: 80px;
}
#\/{
background-color: aquamarine;
}
#\/:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
#\*{
background-color: lightsalmon;
}
#\*:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
#\+{
background-color: cornflowerblue;
}
#\+:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
#\-{
background-color: lightpink;
}
#\-:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
#\={
background-color: rgb(252, 250, 164);
}
#\=:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
#clear,#backspace,#\.,#\37,#\38,#\39,#\34,#\35,#\36,#\31,#\32,#\33,#\30,#invert{
background-color: rgb(234, 238, 255);
}
#clear:active,#backspace:active,#\.:active,#\37:active,#\38:active,#\39:active,#\34:active,#\35:active,#\36:active,#\31:active,#\32:active,#\33:active,#\30:active,#invert:active{
transition: 0.2s;
font-size: medium;
width: 80%;
height: 80%;
}
.swipe-area {
position: absolute;
width: 15px;
left: 0;
top: 0;
height: 100%;
background: #f3f3f300;
z-index: 1;
}
Please forgive me for not providing the online version
.toFixed(2) will give you a string with your decimal place.
var numbers = [
123456,
1234.56,
12345.6
];
numbers.map((item)=>{console.log(item.toFixed(2))});
// 123456.00
// 1234.56
// 12345.60
Okay , so I figured it out!!!
var symbol="";
function openNav(){
document.getElementById("mySidenav").style.width ="100%";
}
function closeNav(){
document.getElementById("mySidenav").style.width ="0px";
}
function getPrevious(){
return document.getElementById("prev").innerText;
}
function setPrevious(num){
document.getElementById("prev").innerText=num;
}
function getCurrent(){
return document.getElementById("curr").innerText;
}
function setCurrent(num){
if(num==""){
document.getElementById("curr").innerText=num;
}
else{
document.getElementById("curr").innerText=getFormattedNumber(num)+symbol;
symbol="";
}
}
function getFormattedNumber(num){
var n = Number(num);
var value = n.toLocaleString("hi-IN");
return value;
}
function reverseNumber(num){
return Number(num.replace(/,/g,''));
}
var operator = document.getElementsByClassName("operator");
var rep=true;
var del;
for(var i=0;i<operator.length;i++){
operator[i].addEventListener("click",function(){
if(this.id=="clear"){
setPrevious("");
setCurrent("");
}
else
if(this.id=="backspace"){
var output=reverseNumber(getCurrent()).toString();
if(output){
output=output.substr(0,output.length-1);
alert(output);
if(output.charAt(output.length-1)=="."){//THIS IS WHERE I MADE THE CHANGES!!!!
rep=true;
}
setCurrent(output);
}
}
else
if(this.id=="invert"){
var output=reverseNumber(getCurrent());
if(output!=""){
output=-output;
setCurrent(output);
}
}
else
if(this.id=="."){
var output=reverseNumber(getCurrent()).toString();
if(output!=""){
if(rep==true){
symbol=this.id;
output=output
setCurrent(output);
rep=false;
}
}
}
else{
var current=getCurrent();
var previous=getPrevious();
if(output=="" && history!=""){
if(isNaN(history[history.length-1])){
history=history.substr(0,history.length-1);
}
}
//write continutation here
}
})
}
var number = document.getElementsByClassName("number");
for(var i=0;i<number.length;i++){
number[i].addEventListener("click",function(){
var output=reverseNumber(getCurrent());
if(output!=NaN){
if(getCurrent().charAt(getCurrent().length-1)=="."){
output=output+(0.1*this.id)
setCurrent(output);
}
else{
output=output+this.id;
setCurrent(output);
}
}
})
}
Thanks to y'all!!
im trying to make a average grade calculator, now thats is going fine but now i want to add a row of inputs every time you click on a button, including html, javascript and also the assigned numbers increased by one. The inputs should go just under the existing ones and the calculate button should move along. I've been stuck at this point for a view days now, appreciate the help!
function getValue(el){
return (+document.getElementById(el).value)
};
function calculator() {
var weight1=getValue('weight-1');
var mark1=getValue('mark-1');
var grade1=weight1*mark1;
var weight2=getValue('weight-2');
var mark2=getValue('mark-2');
var grade2=weight2*mark2;
var totalWeight=weight1+weight2;
var totalGrade=grade1+grade2;
var finalGrade=totalGrade/totalWeight;
var display=document.getElementById('outputDiv');
display.innerHTML='Je gemiddelde is: ' +finalGrade.toFixed(2);
}
body {
font-family: 'Roboto', sans-serif;
background-color: ;
}
h2, h3 {
text-align: center;
}
table {
margin: auto;
}
#table-title {
font-size: 20px;
font-style: italic;
text-align: center;
position: relative;
}
#weight-1, #weight-2 {
width: 100px;
}
input {
text-align: center;
}
#button-div {
position: relative;
width: 150px;
margin: auto;
}
#calc-button {
position: relative;
padding: 5px;
margin-top: 20px;
}
#calc-button:hover {
border-color: black;
box-shadow: 8px 8px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
#outputDiv {
height: 50px;
text-align: center;
width: 300px;
margin: auto;
margin-top: 20px;
}
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<header>
<h2>Gemiddelde cijfer</h2>
<h3>Voer hieronder je cijfers in</h3>
</header>
<body>
<table id="table">
<tr id="table-title">
<td>Weging</td>
<td>Cijfer</td>
</tr>
<tr>
<td><input id="weight-1" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-1" type="text" size=2 maxlength="5" value=""></td>
</tr>
<tr>
<td><input id="weight-2" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-2" type="text" size=2 maxlength="5" value=""></td>
</tr>
</table>
<div id="button-div">
<input id="calc-button" type="button" value="Berekenen je gemiddelde" onclick="calculator()">
</div>
<div id="outputDiv"></div>
</body>
</html>
Instead of hard coding the ids of your inputs, you can get your weight and mark inputs with querySelectoryAll this will get you all your inputs no matter how many there are and then you can loop over the elements to get the values and then append a new input row with the id incremented by 1 to the tbody. Then the next time you call the function there will be one more input to loop over Here's an example based on your code:
function calculator() {
var weight = 0;
var mark = 0;
var weights = document.querySelectorAll('[id^=weight-]');
var grades = document.querySelectorAll('[id^=mark-]');
var trs = document.getElementsByTagName('tr');
var tBody = document.getElementsByTagName('tbody')[0];
var totalWeight = 0;
var totalGrade = 0;
for (var i = 0; i < weights.length; i++) {
totalWeight += +weights[i].value;
}
for (var i = 0; i < grades.length; i++) {
totalGrade += +grades[i].value;
}
var finalGrade=totalGrade/totalWeight;
var display = document.getElementById('outputDiv');
var newTr = document.createElement('TR');
newTr.innerHTML = `<td><input id="weight-${trs.length + 1}" type="text" size=2 maxlength="5" value=""></td><td><input id="mark-${trs.length + 1}" type="text" size=2 maxlength="5" value=""></td>`
tBody.appendChild(newTr);
display.innerHTML='Je gemiddelde is: ' +finalGrade.toFixed(2);
}
body {
font-family: 'Roboto', sans-serif;
background-color: ;
}
h2, h3 {
text-align: center;
}
table {
margin: auto;
}
#table-title {
font-size: 20px;
font-style: italic;
text-align: center;
position: relative;
}
[id^="weight-"] {
width: 100px;
}
input {
text-align: center;
}
#button-div {
position: relative;
width: 150px;
margin: auto;
}
#calc-button {
position: relative;
padding: 5px;
margin-top: 20px;
}
#calc-button:hover {
border-color: black;
box-shadow: 8px 8px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
#outputDiv {
height: 50px;
text-align: center;
width: 300px;
margin: auto;
margin-top: 20px;
}
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<header>
<h2>Gemiddelde cijfer</h2>
<h3>Voer hieronder je cijfers in</h3>
</header>
<body>
<table id="table">
<tr id="table-title">
<td>Weging</td>
<td>Cijfer</td>
</tr>
<tr>
<td><input id="weight-1" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-1" type="text" size=2 maxlength="5" value=""></td>
</tr>
<tr>
<td><input id="weight-2" type="text" size=2 maxlength="5" value=""></td>
<td><input id="mark-2" type="text" size=2 maxlength="5" value=""></td>
</tr>
</table>
<div id="button-div">
<input id="calc-button" type="button" value="Berekenen je gemiddelde" onclick="calculator()">
</div>
<div id="outputDiv"></div>
</body>
</html>
regarding your question and as Barmar said, StackOverflow requires the user to at least attempt the problem, when you run into a wall then you can ask away. I can't overrule this so I can't give you the actual code but I did manage to get it working quite quickly, if you understand Javascript it shouldn't be that difficult. I will try to give you hints on how it really should be structured.
Your Javascript needs to append all your form inputs from the beginning, set a variable equal to 0 and just append on button click a new input field onto the id="table" and increment by 1. This is as far as "I" personally can go to but if you show me that you attempted I can most definitively help you from there.
Best of luck!
This question already has answers here:
How to prevent buttons from submitting forms
(20 answers)
Closed 4 years ago.
Good day!
My form has javascript functions for my buttons for adding rows from table and resetting the text fields in the form itself.
In my add button, I have an incrementing variable, rowCount, for counting the row. It works well and works what I expected. When I put it inside the <form></form> tag, it stuck at 2, doesn't increment and doesn't add rows at all.
Here's my code for our reference and also to be debugged. Thanks in advance!
var rowCount = 1;
function addRow() {
var table = document.getElementById('tblOrder');
rowCount = rowCount + 1;
var row = table.insertRow(rowCount);
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
var cell3 = row.insertCell(3);
var cell4 = row.insertCell(4);
var cell5 = row.insertCell(5);
cell0.innerHTML = "<input type='text'>";
cell1.innerHTML = "<input type='number'>";
cell2.innerHTML = "<input type='text'>";
cell3.innerHTML = "<input type='text'>";
cell4.innerHTML = "<input type='text'>";
cell5.innerHTML = "<input type='text'>";
alert(rowCount)
}
function resetForm() {
document.getElementById('orderForm').reset();
alert('All cleared!')
}
body {
font-family: Calibri;
font-weight: bold;
}
#main {
width: 90%;
border: 1px solid black;
margin: auto;
position: relative;
padding-bottom: 10px;
}
#mainTitle {
text-align: center;
text-decoration: underline;
/* font-weight: bold; */
font-size: 36px;
margin-top: 20px;
margin-bottom: 20px;
}
#cust, #prod {
width: 90%;
position: relative;
margin: auto;
border: 1px solid black;
padding: 20px;
}
#cust {
margin-bottom: 20px;
}
#prod {
margin-bottom: 10px;
}
#custTitle, #prodTitle {
color: blue;
font-size: 25px;
/* font-weight: bold; */
text-decoration: underline;
margin-bottom: 20px;
/* position: relative; */
}
#cust p {
display: block;
}
#cust input {
display: inline;
}
#right {
position: absolute;
top: 0;
right: 0;
padding: 10px;
/* border: 1px solid black; */
}
#right p {
right: 0;
}
#tblOrder {
width: 100%;
border: 1px solid black;
}
#tblOrder thead {
background-color: darkgreen;
color: white;
font-size: 18px;
}
#tblOrder td {
text-align: center;
padding: 5px;
}
#inp1 {
width: 5%;
}
#inp2 {
width: 10%;
}
#inp3, #inp5, #inp6 {
width: 15%;
}
#inp4 {
width: 40%;
}
#tblOrder tr td input {
width: 95%;
}
#add {
color: blue;
font-weight: bold;
background-color: white;
border: 1px solid white;
margin-top: 10px;
}
#foot {
position: relative;
width: 90%;
margin: auto;
/* border: 1px solid black; */
}
#buttons {
position: relative;
left: 0;
/* display: inline; */
}
#total {
position: absolute;
right: 0;
/* display: inline; */
}
<!DOCTYPE html>
<html>
<head>
<title>Forms</title>
<link type='text/css' rel='stylesheet' href='style.css'>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<div id='main'>
<div id='mainTitle'>
Order Form
</div>
<form id='orderForm'>
<div id='cust'>
<div id='custTitle'>
Customer
</div>
<div id='left'>
<p>Customer Name: <input type='text' size=80></p>
<p>Address: <input type='text' size=100></p>
<p>Contact Name: <input type='text' size=80></p>
<p>Phone: <input type='text' size=15>
Mobile: <input type='text' size=15></p>
<p>E-mail Address: <input type='text' size=30>#<input type='text' size=10>.com</p>
</div>
<div id='right'>
<p>Order Date: <input type='text' placeholder='mm/dd/yyyy' size=11></p>
<p>Order Number: <input type='text' size=5></p>
</div>
</div>
<div id='prod'>
<div id='prodTitle'>
Products to Order
</div>
<div id='order'>
<table id='tblOrder'>
<thead>
<td id='inp1'>Unit</td>
<td id='inp2'>Quantity</td>
<td id='inp3'>Product Code</td>
<td id='inp4'>Description</td>
<td id='inp5'>Unit Price</td>
<td id='inp6'>Total Price</td>
</thead>
<tbody>
<tr>
<td><input type='text'></td>
<td><input type='number'></td>
<td><input type='text'></td>
<td><input type='text'></td>
<td><input type='text'></td>
<td><input type='text'></td>
</tr>
</tbody>
</table>
<button id='add' onclick='addRow()'>+Row</button>
</div>
</div>
<div id='foot'>
<div id='total'>
Total: <input type='text' disabled>
</div>
<div id='buttons'>
<button>Submit</button>
<button onclick='resetForm()'>Reset</button>
</div>
</div>
</form>
</div>
</body>
</html>
If buttons are put inside a form, they become submit buttons by default, meaning they submit the form when clicked. If the form doesn't have an action specified, it will be submitted to the same URL as the page, which results in the page being refreshed. This is what happens in your case.
To prevent a button from submitting the form, set its type to be a generic button instead of a submit button:
<button type="button"></button>
I'm trying to implement jstorage on my website. I'm new to JavaScript and thus need some help here.
jstorage requires Key and Value to be given for each saved row.
I would like Key to be filled in automatically with either with auto-increment value or timestamp, whichever is easier - values need to be unique.
Here is the whole code:
<!DOCTYPE html>
<html>
<head>
<title>jStorage - simple JavaScript plugin to store data locally</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
body{
font-family: Sans-serif;
font-size: 13px;
color: #333;
background: blue;
}
a {
color: red;
}
table{
border: 1px solid white;
;
}
h1{
padding-top: 50px;
font-size: 26px;
font-weight: bold;
border-bottom: 3px solid #CDEB8B;
}
h2{
font-size: 18px;
font-weight: bold;
border-top: 1px solid #EEE;
border-bottom: 3px solid #CDEB8B;
margin-top: 20px;
padding-top: 20px;
}
h3{
font-size: 14px;
font-weight: bold;
color: red;
}
td{
padding: 3px;
border-right: 1px solid #CDEB8B;
border-bottom: 1px solid #CDEB8B;
}
thead{
background: url(img/gradientform.png) repeat-x; ;
color: white;
}
.container{
width: 190px;
margin: 10px auto;
}
.delimg {
background: url(img/details_close.png) no-repeat;
width: 20px;
height: 20px;
}
.addimg {
background: url(img/details_open.png) no-repeat;
width: 20px;
height: 20px;
}
</style>
<script src="js/jquery.js"></script>
<script src="static/jquery.json-2.3.js"></script>
<script src="static/jstorage.js"></script>
<script>
function insert_value(){
var row = document.createElement("tr"),
key = document.getElementById('key').value,
val = document.getElementById('val').value;
if(!key){
alert("KEY NEEDS TO BE SET!");
document.getElementById('key').focus();
return;
}
$.jStorage.set(key, val);
document.getElementById('key').value = "";
document.getElementById('val').value = "";
reDraw();
}
function get_value(){
var value = $.jStorage.get(document.getElementById("key2").value);
alert(value);
document.getElementById('key2').value = "";
}
function reDraw(){
var row, del, index;
var rows = document.getElementsByTagName("tr");
for(var i=rows.length-1; i>=0; i--){
if(rows[i].className == "rida"){
rows[i].parentNode.removeChild(rows[i]);
}
}
index = $.jStorage.index();
for(var i=0; i<index.length;i++){
row = document.createElement("tr");
row.className = "rida";
var t = document.createElement("td");
t.innerHTML = index[i];
row.appendChild(t);
t = document.createElement("td");
t.innerHTML = $.jStorage.get(index[i]);
row.appendChild(t);
del = document.createElement("a");
del.href = "javascript:void(0)";
del.innerHTML = "<div class='delimg'></div>";
(function(i){
del.onclick = function(){
$.jStorage.deleteKey(i);
reDraw();
};
})(index[i])
t = document.createElement("td");
t.appendChild(del)
row.appendChild(t);
document.getElementById("tulemused").appendChild(row);
}
}
</script>
<!-- Exception Hub start -->
<script type="text/javascript">
var ehHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + ehHost + "js.exceptionhub.com/javascripts/eh.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
ExceptionHub.setup("b83fb652800fa143596deb6600fd9a2d", 116, 'production');
</script>
<!-- Exception Hub end -->
</head>
<body>
<div class="container">
<table cellspacing="0" cellpadding="0" style="width: 100%">
<thead>
<tr><td width="120">KEY</td><td>VALUE</td><td width="50"> </td></tr>
</thead>
<tbody id="tulemused"></tbody>
<tfoot>
<tr>
<td><input type="text" id="key" name="key" value="" style="width: 110px;" /></td>
<td><input type="text" id="val" name="val" value="" style="width: 98%" /></td>
<td><div class="addimg"></div></td>
</tr>
<tr>
<td><input type="text" id="key2" name="key2" value="" style="width: 110px;" /></td>
<td>Clicking "GET" alerts the value for provided key with the method <em>$.jStorage.get(key)</em></td>
<td>GET</td>
</tr>
</tfoot>
</table>
<script>reDraw()</script>
</body>
</html>
JavaScript:
function uniqid()
{
var newDate = new Date;
return newDate.getTime();
}