-3

I'm trying to get a value in a dropdown to not show if another value is selected. Basically saying if Internal is showing, don't show Record_type_wf or record_type_vr But when adding in the or statement for VR it just breaks and they both always show up.

if((zoneLocation == 'internal' && optionObj[option].value != 'record_type_wf' || 'vanity_type_vr') || zoneLocation != 'internal'){
    g_form.addOption('dns_record_type', optionObj[option].value, optionObj[option].text, optionObj[option].order);

    if (optionObj[option].value == currentRecordType){
        keepCurrentOption = true;
    }
}
cavpollo
  • 4,071
  • 2
  • 40
  • 64
Cody Smith
  • 127
  • 8
  • To those voting to close as a typo: This is not a typo, it's a misunderstanding about what JavaScript's logical operators do. – cdhowie Apr 20 '18 at 19:07
  • Possible duplicate of [JavaScript: Simple way to check if variable is equal to two or more values?](https://stackoverflow.com/questions/12116326/javascript-simple-way-to-check-if-variable-is-equal-to-two-or-more-values) – t.niese Apr 20 '18 at 19:08
  • Also relevant [JavaScript OR (||) variable assignment explanation](https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation) – t.niese Apr 20 '18 at 19:10
  • [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – t.niese Apr 20 '18 at 19:11
  • [Javascript if statement with multiple permissible conditions](https://stackoverflow.com/questions/13944216/javascript-if-statement-with-multiple-permissible-conditions) – t.niese Apr 20 '18 at 19:12

1 Answers1

2

This does not mean what you think it means:

(zoneLocation == 'internal' && optionObj[option].value != 'record_type_wf' || 'vanity_type_vr')

Note that 'vanity_type_vr' is a truthy value, and so this test always succeeds, because it's interpreted like this:

(zoneLocation == 'internal') && (optionObj[option].value != 'record_type_wf') || ('vanity_type_vr')

This entire expression (converted to boolean) simplifies to true because of the last operand.

This is what you meant:

(zoneLocation == 'internal' && optionObj[option].value != 'record_type_wf' && optionObj[option].value != 'vanity_type_vr')
cdhowie
  • 158,093
  • 24
  • 286
  • 300