NepCoder
NepCoder

Reputation: 439

show/hide div based on form value

var test = document.getElementById("TEST1").value;
if (test.value != 1) {
    document.getElementById("field_DOPT").style.display='none';
}

This doesn't seem to work. I am getting the TEST1 value as 1 but its not hiding the div. Any help will be appreciated. thank you

Upvotes: 0

Views: 52

Answers (3)

msutton
msutton

Reputation: 3

You already get the value property of your element when you define test, and then you look for the value property of its value property - so the simplest way to improve this code is:

var testValue = document.getElementById("TEST1").value;
if (testValue != 1) {
    document.getElementById("field_DOPT").style.display='none';
}

I've changed your variable name from test to testValue so that it's clear that you are not actually dealing with the TEST1 element but instead with its value (so that it is clear there is no need to get the value property.

Upvotes: 0

hsz
hsz

Reputation: 152216

You have assigned TEST1 value to the test variable, so in test you have do:

if (test != 1) {
    document.getElementById("field_DOPT").style.display='none';
}

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

In your case test is the value of the input element TEST1, it doesn't have a value property

so either assign the element as the value to test then use test.value in your condition

var test = document.getElementById("TEST1");
if (test.value != 1) {
    document.getElementById("field_DOPT").style.display='none';
}

or use just test in the condition

var test = document.getElementById("TEST1").value;
if (test != 1) {
    document.getElementById("field_DOPT").style.display='none';
}

Upvotes: 1

Related Questions