View Full Version : Javascript/HTML question


dutchfire
Apr 30, 2008, 06:58 AM
I want to have an array of divs, which I can make visible by clicking a button, but it isn't working (and I don't know much about javascript).

<SCRIPT LANGUAGE="JavaScript">
<!--
function bewerknieuwsitem(id) {
document.getElementById('dividers[how to get this id?]').style.visibility = "visible";
document.getElementById('dividers[how to get this id?]').style.height = "400px";
}
// -->
</SCRIPT>

<div style="visibility:hidden; height:0px;" id="dividers[php provided id]">
Yadda yadda</div>

<a href="javascript:bewerknieuwsitem(php provided id);">Button</a>

GVBN
Apr 30, 2008, 05:11 PM
Couldn't you just use

document.getElementById("dividers[" + id + "]").style.visibility = "visible";
document.getElementById("dividers[" + id + "]").style.height = "400px";

warpus
May 02, 2008, 01:49 PM
Just pass in the name of the div id into the function. ie.
<a href="javascript:bewerknieuwsitem('my_div_id');return false;">Button</a>

then in your function you'd just go
document.getElementById(id).style.visibility = "visible";

I stuck that return false; in there so that the link doesn't actually go anywhere.

I'd highly recomment that you use something like mootools to make this into a smooth accordion-style uhh type thing.

tonyf12
May 04, 2008, 04:19 PM
Depending on how you hid it in your css you may have to change
document.getElementById('dividers[how to get this id?]').style.visibility = "visible";
to
document.getElementById('dividers[how to get this id?]').style.display = "block";

Also I would recommend following warpus's instructions to improve your function.

edit: Here is an improved version (type="text/javascript" is better supported than language="javascript" and tags should be lowercase in HTML.

<script type="text/javascript">
<!--
function bewerknieuwsitem(id) {
var element = document.getElementById('dividers[' + id + ']')

element.style.visibility = "visible";
element.style.height = "400px";
}
// -->
</script>