0

I am trying to make a simple calculator. So far, I have been able to do one algorithm (+ - x /) at a time. Now, I want to make a calculator that has a submit menu that can select what you want to do. My code is this:

<input type="text" id="inputone">


<select>

<option>
  +
</option>

<option>
-
</option>

<option>
x
</option>

<option>
/
</option>

</select>

<input type="text" id="inputtwo">

<p> = </p>

<p id="answer"></p>

<script>
 //Some code...
</script>

<button onclick="ad()">Solve</button>

I have researched a bit and I haven't found a good way to do this. Any help would be greatly appreciated, as I have been coding for under a year.

1 Answer 1

1

The <option></option> elements in HTML has an attribute called value that you can use in JavaScript in order to make a condition of what operation you need to do.

For example, if your user selects the * operation. In the click event you are adding to the button, you can get that value through the DOM API with its .value property:

function defineOperation() {
  var operation = document.getElementById('select-id').value;
  // conditions go here to decide what operation to execute
}
2
  • would select-id be the default id of the select? and do I add a onclick="defineOperation()" to the option boxes?
    – Komali
    Mar 22, 2018 at 13:15
  • 1
    select-id would be the id of the select, the click event should be added to the button
    – ivandevp
    Mar 22, 2018 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.