If you are trying to get a single element, then getElementsByClassName
is not the correct method to call because it returns a "live" node list of elements, which is excessive, even in situations where you do want a list of elements.
Instead, since your input
elements can be individually identified by the different classes they each use, querySelector()
is the correct way to get your hands on each of them, individually.
Additionally, don't use inline HTML event handling attributes (onclick
, etc.), here's why.
Now, even when you do get your hands on just the individual elements in question, all HTML elements contain just one type of data, strings. You must convert those strings into numbers before doing math on them.
// Get a reference to the button
var btn = document.getElementById("calc");
// Set up a click event handler for the button
btn.addEventListener("click", myFunction);
function myFunction() {
// Get just the single elements you need and then trim any leading
// or trailing spaces off of the user's input and then convert that
// input into a number:
var x = parseFloat(document.querySelector(".input1").value.trim());
var y = parseFloat(document.querySelector(".input2").value.trim());
var result = x * y;
// Don't use innerHTML when you are suppling HTML as the value
// use textContent instead. This will perform better.
document.getElementById("results").textContent = result;
}
<input type="text" class="input1">
<input type="text" class="input2">
<button id="calc">Calculate</button>
<br>
<p id="results">Results</p>