1

Edit: I've changed the code as follows. Now none of the code seems to work. (Even my the code that did work and I didn't share with you.)

function berekeningAflostabel() {
var zaaksoortMsnp;
var AflostabelPerMaand;

document.getElementById("fldZaakSoortMsnp").addEventListener("change",     function () {
  AflostabelPerMaand = this.value;

  if(AflostabelPerMaand == 1) {
    zaaksoortMsnp = "Tekst1";
  }
  if(AflostabelPerMaand == 2) {
    zaaksoortMsnp = "Tekst2";
  }
  if(AflostabelPerMaand == 3) {
    zaaksoortMsnp = "Tekst3";
  }
  if(AflostabelPerMaand == 4) {
    zaaksoortMsnp = "Tekst4";
  }
  if(AflostabelPerMaand == 5) {
    zaaksoortMsnp = "Tekst5";
  }
  if(AflostabelPerMaand == 6) {
    zaaksoortMsnp = "Tekst6";
  }

  document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp;
  }
}

var eventZaaksoortMsnp = document.getElementById("fldZaakSoortMsnp");
eventZaaksoortMsnp.addEventListener("change", berekeningAflostabel);
5
  • 1
    Hint : = vs ==. Commented Mar 1, 2017 at 11:27
  • You're not calling the function in the code you provided! Commented Mar 1, 2017 at 11:28
  • As an optimisation just set the value to be 0x (x is the number) or at least remove all the ifs and do this: var AflostabelPerMaand = '0' + zaaksoortMsnp.value;! Commented Mar 1, 2017 at 11:30
  • The 01, 02 etc. values for AflostabelPerMaand is a dummy text. If it works, I'll replace them with currency values. Commented Mar 1, 2017 at 11:33
  • @NielsSanders Then look at the answer bellow! Commented Mar 1, 2017 at 11:37

2 Answers 2

1

If you would like to get the selected option's value inside the change listener then you should use: this.options[this.selectedIndex].value

You need to apply the change listener only once. The last two line are unnecessary.

The following code should work:

function berekeningAflostabel() {
  var zaaksoortMsnp;
  var AflostabelPerMaand;

  document.getElementById("fldZaakSoortMsnp").addEventListener("change", function() {
    AflostabelPerMaand = this.options[this.selectedIndex].value;

    if(AflostabelPerMaand == 1) {
      zaaksoortMsnp = "Tekst1";
    }
    if(AflostabelPerMaand == 2) {
      zaaksoortMsnp = "Tekst2";
    }
    if(AflostabelPerMaand == 3) {
      zaaksoortMsnp = "Tekst3";
    }

    document.getElementById("fldMinimaleAfloswaardePerMaand").value = zaaksoortMsnp;
  });
}

berekeningAflostabel();
<select id="fldZaakSoortMsnp">
  <option value="1">teszt</option>
  <option value="2">teszt</option>
  <option value="3">teszt</option>
</select>

<input type="text" id="fldMinimaleAfloswaardePerMaand">

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

4 Comments

The ifs are unnecessary in the first place!
It's seems that OP is looking for this answer not mine!
But you should use == not === as they're not of the same type (one is a string , the other is a number)!
I've edited my original message. (Couldn't add another message with new code.) If you'd like to react, that'll be awesome. Thanks!
1

change your function to this

function () {
    var AflostabelPerMaand = '0' + this.value;
    document.getElementById("fldMinimaleAfloswaardePerMaand").value = AflostabelPerMaand;
}

1 Comment

"The 01, 02 etc. values for AflostabelPerMaand is a dummy text."

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.