1

I have a fairly simple question.

The value of my operationVal gets lost when it enters the click event and if(operationVal == 0 ) returns true. But if i check operationVal just before the event it has the selected value. Furthermore, if i write operation.options[operation.selectedIndex].value instead of operationVal in if in the event, the value grabbed is the correct one instead.

I would be grateful if someone explained me why is that so. I'm assuming it has something to do with JavaScript scope, but it doesn't make sense to me.

const firstNumber = document.getElementById('tbFirstNumber');
const secondNumber = document.getElementById('tbSecondNumber');
const operation = document.getElementById('ddlOperation');
const operationVal = operation.options[operation.selectedIndex].value;
const btn = document.getElementById('btnExecute');
const display = document.getElementById('display');

let result = '';

const regNumbers = /[0-9]{1,}/;

btn.addEventListener('click', function(argument) {
  if (regNumbers.test(firstNumber.value) && regNumbers.test(secondNumber.value)) {
    if (operationVal == 0) {
      alert(operationVal);
      result = 'Operation has not been chosen';
    } else {
      switch (operationVal) {
        case 'add':
          result = 'Result is: ' + (firstNumber.value + secondNumber.value);
          break;
        default:
          // statements_def
          break;
      }
    }
  } else {
    result = 'Number entry is not correct.';
  };

  display.innerHTML = result;
});
3
  • callback function is asynchronous! Commented Feb 21, 2018 at 10:24
  • We don't have the full picture, but it looks like you are trying to read the user's selected choice before they've had an opportunity to make one? Commented Feb 21, 2018 at 10:24
  • @AndyG you are right. That was the mistake. Commented Feb 21, 2018 at 10:33

4 Answers 4

2

The following line:

operationVal = operation.options[operation.selectedIndex].value;

gets the selected value at the time this code executes, not at the future time when the user clicks the button.

The simple solution is to move this line inside the event listener callback function.

Sign up to request clarification or add additional context in comments.

Comments

0

But if i check operationVal just before the event it has the selected value. Furthermore, if i write operation.options[operation.selectedIndex].value instead of operationVal in if in the event, the value grabbed is the correct one instead.

Looks like your value got changed before the click event

const operationVal = operation.options[operation.selectedIndex].value;

since above line has probably been executed while binding events on your page.

Once operationVal is set, it won't get refreshed automatically when you will select a different value from the drop-down.

You need to set this value again in the event handler

btn.addEventListener('click', function(argument) {
  //observe this line
  operationVal = operation.options[operation.selectedIndex].value;
  if (regNumbers.test(firstNumber.value) && regNumbers.test(secondNumber.value)) {
    if (operationVal == 0) {
      alert(operationVal);
      result = 'Operation has not been chosen';
    } else {
      switch (operationVal) {
        case 'add':
          result = 'Result is: ' + (firstNumber.value + secondNumber.value);
          break;
        default:
          // statements_def
          break;
      }
    }
  } else {
    result = 'Number entry is not correct.';
  };

  display.innerHTML = result;
});

1 Comment

I understand it now. Thanks for a more detailed response.
0

The operarionVal was not selected by the user when it is setted. So you have to put the attribution const operationVal = operation.options[operation.selectedIndex].value; inside the function.

Comments

0

operationVal value is set when you load the page. and is not updated with respect when your selectbox value changes therefore the value of operationVal remains constant

You need to move

const operationVal = operation.options[operation.selectedIndex].value;

to click event listner may solve the issue

const firstNumber = document.getElementById('tbFirstNumber');
const secondNumber = document.getElementById('tbSecondNumber');
const operation = document.getElementById('ddlOperation');
let operationVal = operation.options[operation.selectedIndex].value;
const btn = document.getElementById('btnExecute');
const display = document.getElementById('display');

let result = '';

const regNumbers = /[0-9]{1,}/;

btn.addEventListener('click', function(argument) {
  if (regNumbers.test(firstNumber.value) && regNumbers.test(secondNumber.value)) {
    operationVal = operation.options[operation.selectedIndex].value;
    if (operationVal == 0) {
      alert(operationVal);
      result = 'Operation has not been chosen';
    } else {
      switch (operationVal) {
        case 'add':
          result = 'Result is: ' + (firstNumber.value + secondNumber.value);
          break;
        default:
          // statements_def
          break;
      }
    }
  } else {
    result = 'Number entry is not correct.';
  };

  display.innerHTML = result;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.