0

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.

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Taywin
0

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

0

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.

0

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?

Edited by Thirusha: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.