JavaScript Statements

I have a value that works like this.

If I get a value of var1 = 1 then I place a value of TOP in Variable2.
If the value of Var1 is changing from 1 to something else i need to put a null value
but if the value is changing from a value that is not a 1 to another value that is not a 2 i need to keep the value of variable2

Any ideas on how to do this?

TOP is not a value. You mean you put the string “TOP” in variable2?

var var1 = 1;
var var2 = "TOP";
//Do some other stuff.
var1 = 6;
//var2 should be set to null.
var1 = 8;
//var2 should... stay null?

I suppose i need more information, based on that.
there are two changes you describe.
var1 is 1, changes to anything else. Set var2 to null.
var1 is not 1, changes to something that is not 2. Set var2 to… var2? (Which is to say, do nothing)
There are more scenarios to consider.
if var1 is 1, and changes to a 2, what do you do?
if var1 is not 1, and changes to a 2, what do you do?

I also feel like you’re overcomplicating a Math.max function somewhere.

Its more like this.
I have a drop down with the values 1 to 10.

Whatever I put I put the value on Item2 but only if value 1 = 7.
If the value pick goes from 7 to something else, the value needs to be null on Item2.
If the value goes from 1 to 2 or 3 or 4 or 5 or any other number except 7 but item2 has a value then i need to keep that value.
So in summary if goes from 7 to any number but a null.
If it goes from any number to 7 put a 7
But if it goes from any number except 7 if theres a value on item 2 leave it.

Hope that makes it clearer.
Right now I dont know if I need to send like an old value or new or what to do.

So “item” is another input element…?

<select id="dropdown">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

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

Basically yes, you need to store the old value like so:

const dropdown = document.getElementById('dropdown')
const item = document.getElementById('item')

let oldValue = dropdown.value

dropdown.addEventListener('change', () => {
  const currentValue = Number(dropdown.value)

  if (oldValue === 7) {
    // value was 7
    item.value = null
  } else if (currentValue === 7) (
    // value is now 7
    item.value = currentValue
  )

  oldValue = currentValue
})

(If “item” is just a regular variable it would work similarly, just without the .value property.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.