-1

I keep getting errors when debugging in IE. This is a basic hide function.

idHide is the id of the field to be hidden idCondition is the id of the reference field the condition is compared upon value is the value that would satisfy the condition

function hideOnCondition(idHide, idCondition, value)
{    
    if (document.getElementById[idCondition] = value)
    {    
        document.getElementById(idHide).style.display = "none";         
    }
    else
    {           
        document.getElementById(idHide).style.display = "";
    }
}

I always encounter the error in:

if (document.getElementById[idCondition] = value)

"the value of the property is null or undefined not a function object"

Then I tried changing "getElementById" with "all". then changed the brackets to parentheses, still nothing, only for the line to be highlighted in yellow.

Im sorry, I'm just stumped. Again, thank you all for understanding.

4
  • 2
    = should be ==. Also I have to admit I've never seen document.getElementById treated like an associative array Commented Mar 28, 2018 at 10:00
  • Do you know that comparison uses ==? Commented Mar 28, 2018 at 10:00
  • What is the syntax error? Please share a working snippet using <> in your toolbar. Commented Mar 28, 2018 at 10:15
  • There is no syntax error, there is a runtime error and it is listed in the question. The question's title is incorrect. Commented Mar 28, 2018 at 10:16

4 Answers 4

1
  • You were using square brackets instead of parentheses
  • === should be used for comparing not =

.

function hideOnCondition(idHide, idCondition, value)
{    
    if (document.getElementById(idCondition) === value) // <- fix here
    {    
        document.getElementById(idHide).style.display = "none";         
    }
    else
    {           
        document.getElementById(idHide).style.display = "";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

that I did not know, I was searching for other stuff about the code, but I didn't realize it was the equal sign. Thank you very much.
@ReginaldMaravilla you're welcome, if this matches what you need then please accept it as an answer
1
function myFunction(option, value, div) {

    //get the element you want to hide by it's ID
    var x = document.getElementById(div); 

    //if the option you selected is coresponding to the given value 
    //hide the earlier selected element
    if (option === value) {
        x.style.display = "none";
    } else {
        x.style.display = "block";
    }
}

This should do it.

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Thank you, I think I get what your code does. Ill try to apply it to mine,
0

Instead of an assignment you should use the comparison operator Identity / strict equality (===):

function hideOnCondition(idHide, idCondition, value) {
    const result = document.getElementById[idCondition] === value ? 'none' : '';
    document.getElementById(idHide).style.display = result;
}

Comments

0

Two issues I could see

  • using = instead of === and not comparing value instead only comparing the the output of document.getElementById[idCondition] with value.

  • using [] instead of invoking the function using ()

Although, none of these would cause the syntax error as you have claimed in your post.

You can simplify it as

var getEl = (id) => document.getElementById(id);
function hideOnCondition(idHide, idCondition, value)
{    
    getEl(idHide).style.display = getEl(idCondition).value == value ? "none" : "";
}

Comments

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.