0

My java script, which I got from a stack overflow question looks like this:

<script type="text/javascript">
function search() {
    var a = document.getElementByClassName("s1");
    var b = a.options[a.selectedIndex].value;
    if (b = 1) {
        document.getElementById("vmh").style.visibility = hidden
    }
    }
</script>

and there is a button that activates the function search, but the visibility is not changing

Alex
  • 23
  • 1
  • 8
  • 1
    Can you also include the markup for the `select` tag? – Matt U Nov 29 '19 at 17:39
  • div class="title"> – Alex Nov 29 '19 at 17:41
  • 1
    `getElementByClassName` returns a **list** of elements, not just one element ([more](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return)). Also note that `=` is **assignment**, not comparison ([more](https://stackoverflow.com/questions/11178605/why-doesnt-my-simple-if-statement-render-false-in-javascript)). And `value` is always a string ([more](http://stackoverflow.com/questions/32945779/incorrect-result-in-javascript-calculation/32945816#32945816)). Finally, unless `hidden` is a variable, it should be in quotes: `"hidden"`. – T.J. Crowder Nov 29 '19 at 17:47
  • 1
    If you got that function from a stack overflow question, I sure hope the answers explained the issues with it. – T.J. Crowder Nov 29 '19 at 17:49

1 Answers1

1

Your if statement is not comparing values, but is instead assigning a value. b = 1 assigns 1 to the variable b. Instead, you'll want to use a comparison operator if (b == 1).

Secondly, the hidden should be wrapped in quotes. Currently you are assigning hidden to the visibility property, but hidden does not appear to be a variable. Instead, you'll do visibility = 'hidden'.

Matt U
  • 4,970
  • 9
  • 28