Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare equals '=' in javascript

I am doing a calculator program in javascript and HTML. I have a button with a value '=' in it. In that one when a user click on "=" button in calculator. it should start evaluate the input he/she had given so far. But when a user click '=' button i tried to compare like this

$(".calcinput").click(function () {
        var res = $(this).val();
        if (res == '=') {
            //code to handle when '=' is pressed;
        }
   }

but it didn't work please help me.

like image 415
Ashok kumar Avatar asked Jun 07 '26 18:06

Ashok kumar


1 Answers

Your code is good enough to handle the event .. Just add an extra = in comapre

$(".calcinput").click(function () {
    var res = $(this).val();
    //if (res == '=') {
      if (res === '=') {
        //code to handle when '=' is pressed;
    }
});

Next check where you used div or button or something else element to represent the button =

If it is button then use val() to extract the value

var res = $(this).val();

If the element is div then it could be innerHTML or innerTEXT

 var res = $(this).innerHTML;
 //Or
 var res = $(this).innerText;

You can add an extra line to check whether you getting proper value of element

var res = $(this).val();
alert(res);
like image 162
Moumit Avatar answered Jun 10 '26 08:06

Moumit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!