user14186487
user14186487

Reputation:

Retrieve text after clicking a button in calculator project JS

My query: Can you tell me a event through which when I click any button its text should get copied in var buttonText, Ex when I click 8 on calculator, "8" should get copied in the variable buttonText.

I have tried var buttonText = buttons[i].innerText. On the 6th line of my JavaScript code, but seems wrong and it's not working. Please help. I have also used query selector and for loop to get all buttons.

let screen = document.getElementById('screen'); // selects the screen
var buttons = document.querySelectorAll('button'); // selects all the buttons 
let screenValue = ""; // inital value on the screen is empty
for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener('click', function() {
    var buttonText = buttons[i].innerText. //  Gets the text of that button which is clicked 

    if(buttonText == 'X') { // since the cross X sign is * multiplication sign.
      buttonText = '*';
      screenValue += buttonText;
      screen.value = screenValue;
    }
    else if (buttonText == 'C') { // for clearing the screen
      screenValue = "";
      screen.value = screenValue;
    } else if (buttonText == '=') { // = for calculating the arithmetic operation.
      screen.value = eval(screenValue);
    } else {
      screenValue += buttonText; // for appending inputs together
      screen.value = screenValue;
    }

  });
}
<div class="container">
  <h1>Calculator By Ayush</h1>

  <div class="calculator">
    <input type="text" name="screen" id="screen">
    <table>
      <tr>
        <td><button>(</button></td>
        <td><button>)</button></td>
        <td><button>C</button></td>
        <td><button>%</button></td>
      </tr>
      <tr>
        <td><button>7</button></td>
        <td><button>8</button></td>
        <td><button>9</button></td>
        <td><button>X</button></td>
      </tr>
      <tr>
        <td><button>4</button></td>
        <td><button>5</button></td>
        <td><button>6</button></td>
        <td><button>-</button></td>
      </tr>
      <tr>
        <td><button>1</button></td>
        <td><button>2</button></td>
        <td><button>3</button></td>
        <td><button>+</button></td>
      </tr>
      <tr>
        <td><button>0</button></td>
        <td><button>.</button></td>
        <td><button>/</button></td>
        <td><button>=</button></td>
      </tr>
    </table>
  </div>
</div>

Upvotes: 0

Views: 521

Answers (2)

mplungjan
mplungjan

Reputation: 178350

  • Typo (full stop after innerText)

  • scope issue (buttons[i] should be this)

Code after change:

 buttons[i].addEventListener('click', function() {
    var buttonText = this.innerText; //  Gets the text of that button which is clicked 

You can add using a closure adding 'click' event listeners in loop

but here is a simpler delegated click version with a backspace

const screen = document.getElementById("screen");
let screenValue = ""; // inital value on the screen is empty
document.getElementById("calculator").addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.tagName !== "BUTTON") return;

  var buttonText = tgt.innerText; //  Gets the text of that button which is clicked 

  if (buttonText == 'X') { // since the cross X sign is * multiplication sign.
    buttonText = '*';
    screenValue += buttonText;
    screen.value = screenValue;
  } else if (buttonText == 'CA') { // for clearing the screen
    screenValue = "";
    screen.value = screenValue;
  } else if (buttonText == '🡄') { // for clearing last entered
    screenValue = screenValue.slice(0,-1);
    screen.value = screenValue;
  } else if (buttonText == '=') { // = for calculating the arithmetic operation.
    screen.value = eval(screenValue);
  } else {
    screenValue += buttonText; // for appending inputs together
    screen.value = screenValue;
  }

})
button { height: 40px; width:40px; }
<div class="container">
  <h1>Calculator By Ayush</h1>

  <div id="calculator">
    <input type="text" name="screen" id="screen"> <button>🡄</button>
    <table>
      <tr>
        <td><button>(</button></td>
        <td><button>)</button></td>
        <td><button>CA</button></td>
        <td><button>%</button></td>
      </tr>
      <tr>
        <td><button>7</button></td>
        <td><button>8</button></td>
        <td><button>9</button></td>
        <td><button>X</button></td>
      </tr>
      <tr>
        <td><button>4</button></td>
        <td><button>5</button></td>
        <td><button>6</button></td>
        <td><button>-</button></td>
      </tr>
      <tr>
        <td><button>1</button></td>
        <td><button>2</button></td>
        <td><button>3</button></td>
        <td><button>+</button></td>
      </tr>
      <tr>
        <td><button>0</button></td>
        <td><button>.</button></td>
        <td><button>/</button></td>
        <td><button>=</button></td>
      </tr>
    </table>
  </div>
</div>

Upvotes: 1

Rayees AC
Rayees AC

Reputation: 4659

Firstly,

  • Change the onclik event of buttons. there is not get object buttons[i] use here this to get the object.

Second,

  • innerText. you are used a dot(.) after innerText please changes to semi column(;)

let screen = document.getElementById('screen');   // selects the screen
var buttons = document.querySelectorAll('button'); // selects all the buttons 
let screenValue = "";  // inital value on the screen is empty
for(var i=0;i<buttons.length;i++)
  {
    buttons[i].addEventListener('click',function()  
    {
        var buttonText =  this.innerText;  //  Gets the text of that button which is clicked 
        
        if(buttonText == 'X') {   // since the cross X sign is * multiplication sign.
            buttonText = '*';
            screenValue += buttonText;
            screen.value = screenValue;
        }
        else if (buttonText == 'C') {   // for clearing the screen
            screenValue = "";
            screen.value = screenValue;
        }
        else if (buttonText == '=') {     // = for calculating the arithmetic operation.
            screen.value = eval(screenValue);
        }
        else {
            screenValue += buttonText;   // for appending inputs together
            screen.value = screenValue; 
        }

    });
}
<div class="container">
            <h1>Calculator By Ayush</h1>
    
            <div class="calculator">
                <input type="text" name="screen" id="screen">
                <table>
                    <tr>
                        <td><button>(</button></td>
                        <td><button>)</button></td>
                        <td><button>C</button></td>
                        <td><button>%</button></td>
                    </tr>
                    <tr>
                        <td><button>7</button></td>
                        <td><button>8</button></td>
                        <td><button>9</button></td>
                        <td><button>X</button></td>
                    </tr>
                    <tr>
                        <td><button>4</button></td>
                        <td><button>5</button></td>
                        <td><button>6</button></td>
                        <td><button>-</button></td>
                    </tr>
                    <tr>
                        <td><button>1</button></td>
                        <td><button>2</button></td>
                        <td><button>3</button></td>
                        <td><button>+</button></td>
                    </tr>
                    <tr>
                        <td><button>0</button></td>
                        <td><button>.</button></td>
                        <td><button>/</button></td>
                        <td><button>=</button></td>
                    </tr>
                </table>
            </div>
        </div>

    

Upvotes: 0

Related Questions