radio button checked javascript – no jquery

below function seems to be not working..

function myCalculator () {

    var operator = document.getElementsByName("operator");
    var output = document.getElementById("output");
    var firstDig = parseInt(document.getElementById("firstDig").value);
    var secondDig = parseInt(document.getElementById("secondDig").value);   

    for (var i=0; i<operator.length; i++){

        var currentOperator = operator[i].id;

        if(currentOperator.checked == "Add"){
            var add = firstDig + secondDig;
                output = add;
        };


    };

        }

somehow the “if” parameter is returning false but, i got the element with the property checked…im making a simple calculator…below is the html…

<fieldset>

    <label>Enter Number:</label>
    <input type="text" id="firstDig" />
    <input type="text" id="secondDig" />
        <br/> <br/>
    <label>Select Operator:</label>
    <br/><br />

    <input type="radio" id="Add" name="operator" checked="checked" />
        <label for = "Add">Add</label>

    <input type="radio" id="Subt" name="operator" />
        <label for = "Subt">Subtract</label>

    <input type="radio" id="Divi" name="operator" />
        <label for = "Divi">Divide</label>

    <input type="radio" id="Multi" name="operator" />
        <label for = "Multi">Multiply</label>

</fieldset>


<fieldset>
    <p>RESULT</p>
    <input type="text" id="output" />
    <button type="button" onclick="myCalculator()">Calculate</button>
</fieldset>

im still learning the foundations of javascript so im not that inetrested in jquery at the moment…all answers are appreciated…thanks


Source: New Questions

Leave a Reply