Reputation: 987
Hi I am really new to javascript and struggling with getting this code working. So when I click the button after filling the inputs with numbers, nothing happens. Do you know what I am doing wrong in here?
HTML:
<input type="text" id="eing1" placeholder=" z. B. 700€" style="width:80px;">
<input type="text" id="eing2" placeholder=" z. B. 30000€" style="width:80px;">
<input type="text" id="eing3" placeholder=" ∅ 225" style="width:80px;">
<input id="button" type="button" value="Berechnen" onClick="ausgeben()">
<p id="arbeitszeit"></p>
<p id="produktivitaetssteigerung"></p>
<p id="amortisationszeit"></p>
Javascript:
function ausgeben(){
var kostentisch = parseInt(document.getElementById("eing1").value)
var bruttogehalt = parseInt(document.getElementById("eing2").value)
var arbeitstage = parseInt(document.getElementById("eing3").value)
var stundenlohn = bruttogehalt/arbeitstage/8;
var arbeitszeit = arbeitstage*8;
var produktivitaetssteigerung = arbeitszeit*0.12;
var amortisationszeit = kostentisch/(arbeitstage/(produktivitaetssteigerung*stundenlohn));
document.getElementById("stundenlohn").innerHTML=tischsitzen + " Stunden";
document.getElementById("produktivitaetssteigerung").innerHTML=armlehne + " Stunden";
document.getElementById("amortisationszeit").innerHTML=stuhl + " Tage";
}
Here the js-fiddle: https://jsfiddle.net/1ej6ezou/
Upvotes: 0
Views: 49
Reputation: 1723
First, I noticed that in the JavaScript part your referencing to the element ID stundenlohn
that doesnt exist in any HTML tag shown.
For the button, I would personally give a class name or a new attribte like <input type="text" data-event-target id="..." value="..." />
to the elements from which I want to catch the event. Once I gived the target elements a new attribute (data-enter-target
in this case) I would use JS to add an event listener on each of them.
<style>
/* I put the style outside the element
* so if I need to change a value, the
* changes will apply to all the elements
* that has this class.
*/
* {color: black;}
.w-80 {
width: 80px;
}
p {text-transform: uppercase; font-weight: bold;}
.color-0 {background-color: #0FA;}
.color-1 {background-color: #FF0;}
.color-2 {background-color: #F0F;}
.color-3 {background-color: #0F0;}
.color-4 {background-color: #00F;}
</style>
<div class="container">
<!-- Here are the three input on which I added -->
<!-- the "data-enter-target" attribute -->
<input type="text" data-enter-target id="input-1" class="w-80" value="Value Here" />
<input type="text" data-enter-target id="input-2" class="w-80" value="Second value" />
<input type="text" data-enter-target id="input-3" class="w-80" value="Third value" />
<!-- Other elements like paragraphs -->
<p></p>
<p></p>
<p></p>
<p>Click several times</p>
<!-- Add the submit button and add it an onclick function -->
<input type="button" id="myButton" value="Submit" onclick="btnSubmit()"/>
</div>
<script>
// Retrieve all the elements that contains the attribute
// and store them in a variable as an array.
var targets = document.querySelectorAll('input[data-enter-target]');
// Then for each element
targets.forEach(function(current_element, index) {
// Add the event listener on the element
current_element.addEventListener("keydown", function(event) {
// Check if the key pressed is 'Enter'
if(event.key == "Enter") { // You could also use the keycode (Enter=13)
// Do the thing you wanna do when Enter is pressed
// In this case we wanna trigger the onclick event of the submit button
document.getElementById('myButton').click();
}
});
});
// Add the function called when the button is clicked
function btnSubmit(event) {
// Do whatever you want when the button is clicked
alert('Hey!!! The button has been clicked!');
// Ex: You could replace or add content to an existing element
document.querySelectorAll('p').forEach(function(element) {
element.innerHTML='<span class="color-'+parseInt(Math.random()*5)+'">Bob loves potatoes!</span>';
});
}
</script>
Upvotes: 0
Reputation: 5778
Bind keyUp
listener as below for more info find snippet below
var selectors = document.querySelectorAll("#eing1, #eing2, #eing3");
for (var i = 0; i < selectors.length; i++) {
selectors[i].addEventListener('keyup', function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("button").click();
}
});
}
<div id="ergonomierechner">
<p id="h1" style="text-align: center; font-size: 150%; font-weight: bold; margin-top: 5px;">Individueller Ergonomie-Rechner</p>
<center><p id="text" >
<span>Kosten für Sitz-Stehschreibtisch: </span>
<input type="text" id="eing1" placeholder=" z. B. 700€" style="width:80px;">
<span>Bruttogehalt inkl. 20% Lohnnebenkosten: </span>
<input type="text" id="eing2" placeholder=" z. B. 30000€" style="width:80px;">
<span>Jährliche Arbeitstage: </span>
<input type="text" id="eing3" placeholder=" ∅ 225" style="width:80px;">
<input id="button" type="button" value="Berechnen" onClick="ausgeben();">
</p></center>
<div id="fehler"></div>
<table style="width:100%">
<tr>
<th>Arbeitszeit:</th>
<th>Produktivitätssteigerung pro Tag (12%):</th>
<th>Amortisationszeit:</th>
</tr>
<tr style="text-align: center;">
<td><p id="arbeitszeit"></p></td>
<td><p id="produktivitaetssteigerung"></p></td>
<td><p id="amortisationszeit"></p></td>
</tr>
</table>
</div>
<script>
function ausgeben(){
var kostentisch = parseInt(document.getElementById("eing1").value)
var bruttogehalt = parseInt(document.getElementById("eing2").value)
var arbeitstage = parseInt(document.getElementById("eing3").value)
var stundenlohn = bruttogehalt/arbeitstage/8;
var arbeitszeit = arbeitstage*8;
var produktivitaetssteigerung = arbeitszeit*0.12;
var amortisationszeit = kostentisch/(arbeitstage/(produktivitaetssteigerung*stundenlohn));
//Changes to be made here
document.getElementById("arbeitszeit").innerHTML=arbeitszeit + " Stunden";
document.getElementById("produktivitaetssteigerung").innerHTML=produktivitaetssteigerung + " Stunden";
document.getElementById("amortisationszeit").innerHTML=amortisationszeit + " Tage";
}
</script>
Upvotes: 1