Hello I am an aspiring coder and have been making a ton of progress on my proof of concept i.e. simple web calculator but I am having trouble with my disableInputFields function. I cannot figure out how to combine my two if statements into one conditional statement. I need to make my calculate button no longer disabled based off having a number in both input fields and having one radio button selected. I tried making two conditionals but the logic doesn’t work how I intend it to.
<!DOCTYPE html>
<head>
<title>WebCalc</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="./webcalc.css" />
</head>
<body>
<h1>Simple Calculator</h1>
<form>
<label for="input1" id="put1">Input 1</label>
<input id="input1" onkeyup="disableInputFields()" type="number"><br>
<div class="buttons">
<label>
<input id="btnAdd" class="radioButton" type="radio" name="radiobruh" onclick="disableInputFields()"></input>
<span>+</span>
</label>
<label>
<input id="btnSubtract" class="radioButton" type="radio" name="radiobruh" onclick="disableInputFields()"></input>
<span>-</span>
</label>
<label>
<input id="btnMultiply" class="radioButton" type="radio" name="radiobruh" onclick="disableInputFields()"></input>
<span>*</span>
</label>
<label>
<input id="btnDivide" class="radioButton" type="radio" name="radiobruh" onclick="disableInputFields()"></input>
<span>/</span>
</label>
</div>
<label for="input2" id="put2">Input 2</label>
<input id="input2" onkeyup="disableInputFields()" type="number"><br>
<div>
<button id="calculate" type="button" disabled>Calculate</button>
</div>
<p>Output</p>
<textarea id="resultTxtArea" rows="5" cols="75" ></textarea>
<div>
<button id="clear" type="default">Clear</button>
</div>
</form>
</body>
<script>
// radio buttons have to be selected
// numbered outputs
let calculationInput1 = document.getElementById("input1");
let calculationInput2 = document.getElementById("input2");
let txtAreaResult = document.getElementById("resultTxtArea");
let additionRadioButton = document.getElementById("btnAdd");
let subtractRadioButton = document.getElementById("btnSubtract");
let multiplyRadioButton = document.getElementById("btnMultiply");
let divideRadioButton = document.getElementById("btnDivide");
let allButtons = document.getElementsByClassName("buttons");
let calculationEventListener = document.getElementById("calculate");
let clearButton = document.getElementById("clear");
let calculation;
calculationEventListener.addEventListener("click", function() {
console.dir(additionRadioButton);
compute();
outputResult(calculation);
});
function disableInputFields() {
if (calculationInput1.value && calculationInput2.value === "") {
calculationEventListener.disabled = true;
} else {
calculationEventListener.disabled = false;
}
if (additionRadioButton.checked === "true" ||
subtractRadioButton.checked === "true" ||
multiplyRadioButton.checked === "true" ||
divideRadioButton.checked === "true" ) {
calculationEventListener.disabled = true;
} else {
calculationEventListener.disabled = false;
}
};
function compute() {
if (additionRadioButton.checked === true){
calculation = calculationInput1.valueAsNumber + calculationInput2.valueAsNumber;
}
if (subtractRadioButton.checked === true){
calculation = calculationInput1.valueAsNumber - calculationInput2.valueAsNumber;
}
if (multiplyRadioButton.checked === true){
calculation = calculationInput1.valueAsNumber * calculationInput2.valueAsNumber;
}
if (divideRadioButton.checked === true){
calculation = calculationInput1.valueAsNumber / calculationInput2.valueAsNumber;
}
};
function outputResult(result) {
txtAreaResult.innerHTML += new Date() + ": " + result + 'rn';
};
</script>
</html>
I’ve tried combining the conditions together with the pipe operators but it doesn’t work as one radio button will make the button able to be pressed without anything in the input fields.
2
Answers
As I understand, this is what you want:
So your disableInputFields function should look like this:
I just tried this and it works perfectly fine.
So simple by using
names
inside a<form>