Hello to all.
The task. Expression evaluation. For example, the page has
аргумент1 оператор аргумент2 ответ
The operator must be in the form of a drop-down list. How to take a value in the form of javascript? It is the value of the operator, for example, if the "+" sign, then add argument1 + argument2, if the minus sign, then argument1-argument2
function f1(obj) { var a = obj.st1.value; var h = obj.st2.value; var k = obj.list.value; k = parseInt(k); switch (k) { case 0: var s = "ошибка!"; break; case 1: var s = (a + h); break; case 2: var s = (a - h); break; default: alert("error!"); } document.write("Выражение равно: ", s); return s; }
<p>Вычисление</p> <form name="forml"> arg1: <input type="text" size="7" name="st1"> <hr> arg2: <input type="text" size="7" name="st2"> <hr> <select name="list" size=1> <option value="1">+</option> <option value="1">-</option> </select> <input type="button" value=Вычислить onClick="f1(forml)"> </form>
getElementById
gets the elements from the form, I need the opposite - get the elements from Javascript.