Ad
Concatenate Two Different Select/option Tag To Able To Calculate In JavaScript
I try to make some calculating method in Js for calculate the perimeter and the area of the square/rectangle.
I failed somewhere in the options when I pick the different methods.
I want to work when I choose square of the area or perimeter to calculate after I apply the input fields with two different number.
I also try to hide the second input field when I only choose the square option, but I could not do it.
So any helpful suggestion to how to fix my code to work properly?
function calc() {
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
var typ = document.getElementById("type").value;
var squarePerimeter = num1 * num1;
document.getElementById("perimeter").value = squarePerimeter;
var squareArea = num1 * 4;
document.getElementById("area").value = squareArea;
var rectanglePerimeter = num1 * num2;
document.getElementById("perimeter").value = rectanglePerimeter;
var rectangleArea = 2 * num1 + num2;
document.getElementById("area").value = rectangleArea;
if (typ === "sqr") {
document.getElementById("result").value = squarePerimeter;
} else if (typ === "rect") {
document.getElementById("result").value = rectanglePerimeter;
} else if (typ === "sqr") {
document.getElementById("result").value = squareArea;
} else if (typ === "rect") {
document.getElementById("result").value = rectangleArea;
}
};
<select name="" id="type">
<option id="square" value="sqr">square</option>
<option id="rectamgle" value="rect">rectangle</option>
</select>
<select name="" id="calculating">
<option id="perimeter" value="perimeter">Perimeter</option>
<option id="area" value="area">Area</option>
</select>
<br>
<input type="number" id="num1">
<input type="number" id="num2">
<br>
<button type="button" onclick="calc()">Calculate</button>
<input type="number" id="result">
Ad
Answer
Following your code structure you should change some things, like yoiur elseif
, your are setting the same conditions as previous ifs
, they will never be true.
function calc() {
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
var typ = document.getElementById("type").value;
var obj = document.getElementById("calculating").value;
if (typ === "sqr" && obj === "area") {
document.getElementById("result").value = num1 * num1;
} else if (typ === "rect" && obj === "area") {
document.getElementById("result").value = num1 * num2;
} else if (typ === "sqr") {
document.getElementById("result").value = num1 * 4;
} else if (typ === "rect") {
document.getElementById("result").value = 2 * (num1 + num2);
}
};
function toggleNum2() {
var typ = document.getElementById("type").value;
var num2 = document.getElementById("num2");
if (typ === "sqr") {
num2.style.display = "none";
} else {
num2.style.display = "inline-block";
}
};
<body onload="toggleNum2()">
<select name="" id="type" onchange="toggleNum2()">
<option id="square" value="sqr">square</option>
<option id="rectamgle" value="rect">rectangle</option>
</select>
<select name="" id="calculating">
<option id="perimeter" value="perimeter">Perimeter</option>
<option id="area" value="area">Area</option>
</select>
<br>
<input type="number" id="num1">
<input type="number" id="num2">
<br>
<button type="button" onclick="calc()">Calculate</button>
<input type="number" id="result">
</body>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad