I have a Javascript function that dynamically creates a box with document.getElementById('lightbox').innerHTML. In the box will be, among other things, a div with id="div1" and visibility="hidden" and a form which, onSubmit, calls the function myFunction.

The function basically looks like

myFunction() {

document.getElementById('div1').style.visibility = 'visible';

}

which has worked just fine up to this point.
Now I'm trying to make the function dynamic by passing the variable "div1" into the function and make it look like

myFunction(var1) {

document.getElementById(var1).style.visibility = 'visible';

}

but it keeps giving me the error "document.getElementById(var1) is null".

When looking through the source code I can't find the "div1" div since it was created with Javascript, and I assume this is the problem. Yet it worked just fine before I tried making the function dynamic.

Halp.

You are calling the function incorrectly, you should call it like this:

<input type="text" id="test" style="visibility:hidden" />
<input type="text" id="test2" onclick="myFunction('test')" />

take note of the single quotes on the id sent into the myfunction function

You are calling the function incorrectly, you should call it like this:

<input type="text" id="test" style="visibility:hidden" />
<input type="text" id="test2" onclick="myFunction('test')" />

take note of the single quotes on the id sent into the myfunction function

I didn't include any calling code in that post lol. That part in the program was done as you've written.

then it should work, it worked for me.

is the div1 div loaded before u run the function. based on what u have provided, one can dynamically make an element visible, as my example showed, if yours is still not working, then it could be something else in your html or script files. can u provide the complete file?

One add on, dynamically set visibility may not work on IE browser unless they have fixed the bug...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.