vendredi 31 janvier 2020

how to make calculator in JQuery when we add new equation that remove old eval value and replace a new button value in JQuery?

I am trying to replace the 'answer' value with new value in my 'jquery' calculator. but it does not replace the answer. rather it adds to next of the 'answer'.strong text

 <input type="text" id="preview">
        <input type="button" type="button" id="btn1" value="3" onclick="showValue()">
        <input type="button" id="btn2" value="4">
        <input type="button" id="btn4" value="*">
        <input type="button" id="btn5" value="+">
        <input type="button" id="btn6" value="-">
        <input type="button" id="btn7" value="/">
        <input type="button" id="btn3" value="=">
        <input type="button" id="btn8" value="bs">

$("#btn1").click(function () {
            $("#preview").val($("#preview").val() + $("#btn1").val())
        });
        $("#btn2").click(function () {
            $("#preview").val($("#preview").val() + $("#btn2").val())
        });
        $("#btn4").click(function () {
            $("#preview").val($("#preview").val() + $("#btn4").val())
        });
        $("#btn5").click(function () {
            $("#preview").val($("#preview").val() + $("#btn5").val())
        });
        $("#btn6").click(function () {
            $("#preview").val($("#preview").val() + $("#btn6").val())
        });
        $("#btn7").click(function () {
            $("#preview").val($("#preview").val() + $("#btn7").val())
        });
        $("#btn8").click(function () {
            $("#preview").val($('#preview').val().substring(0, $('#preview').val().length - 1));
        });

        $("#btn3").click(function () {
            $("#preview").val((eval($("#preview").val()))
            )
        });

Aucun commentaire:

Enregistrer un commentaire