qdh
asked on
Java, CSS: Hide two divs and show another
Hi,
I'm fairly new to Javascript. I'm trying to get a link to hide two divs and show another, and then when clicked again it does the reverse. Here's the code I have used so far:
CSS:
#collapseheaders {
position:absolute;
background-color:#a7a9ac;
padding-top:11px;
bottom:154px;
width:960px;
height:21px;
}
#collapsebodies {
position:absolute;
background-color:#275cab;
bottom:0px;
width:960px;
height:154px;
}
#justheaders {
position:absolute;
background-color:#a7a9ac;
padding-top:11px;
bottom:0px;
width:960px;
height:21px;
display:none;
}
Java:
<script language="javascript">
function toggleDiv(collapsebodies){
if(document.getElementById (collapseb odies).sty le.display == 'none'){
document.getElementById(co llapsebodi es).style. display = 'block';
}else{
document.getElementById(co llapsebodi es).style. display = 'none';
}
}
function toggleDiv(collapseheaders) {
if(document.getElementById (collapseh eaders).st yle.displa y == 'none'){
document.getElementById(co llapsehead ers).style .display = 'block';
}else{
document.getElementById(co llapsehead ers).style .display = 'none';
}
}
</script>
HTML:
<a href="javascript:;" onmousedown="toggleDiv('co llapsebodi es'); toggleDiv('collapseheaders ');">Toggl e Div Visibility</a>
Basically the code works the way it should from the site I got it off. I tried to add another toggleDiv('justheaders'); to the onmousedown area, that would show the third div when the other two are hidden. But then it wouldn't work at all.
I'm just not sure exactly why it's not working. Any help would be much appreciated.
Thanks,
Quintin
I'm fairly new to Javascript. I'm trying to get a link to hide two divs and show another, and then when clicked again it does the reverse. Here's the code I have used so far:
CSS:
#collapseheaders {
position:absolute;
background-color:#a7a9ac;
padding-top:11px;
bottom:154px;
width:960px;
height:21px;
}
#collapsebodies {
position:absolute;
background-color:#275cab;
bottom:0px;
width:960px;
height:154px;
}
#justheaders {
position:absolute;
background-color:#a7a9ac;
padding-top:11px;
bottom:0px;
width:960px;
height:21px;
display:none;
}
Java:
<script language="javascript">
function toggleDiv(collapsebodies){
if(document.getElementById
document.getElementById(co
}else{
document.getElementById(co
}
}
function toggleDiv(collapseheaders)
if(document.getElementById
document.getElementById(co
}else{
document.getElementById(co
}
}
</script>
HTML:
<a href="javascript:;" onmousedown="toggleDiv('co
Basically the code works the way it should from the site I got it off. I tried to add another toggleDiv('justheaders'); to the onmousedown area, that would show the third div when the other two are hidden. But then it wouldn't work at all.
I'm just not sure exactly why it's not working. Any help would be much appreciated.
Thanks,
Quintin
Try renaming one of the functions
Although the coding is correct bt give visibility: hidden instead of display and see if it works..
ASKER
I changed the names, it seems to work a little better. But still not as intended, can you see any problems with my code changes?
I changed the code to the following:
Java:
<script language="javascript">
function toggleDiv1(collapsebodies) {
if(document.getElementById (collapseb odies).sty le.display == 'none'){
document.getElementById(co llapsebodi es).style. display = 'block';
}else{
document.getElementById(co llapsebodi es).style. display = 'none';
}
}
function toggleDiv2(collapseheaders ){
if(document.getElementById (collapseh eaders).st yle.displa y == 'none'){
document.getElementById(co llapsehead ers).style .display = 'block';
}else{
document.getElementById(co llapsehead ers).style .display = 'none';
}
}
function toggleDiv3(justheaders){
if(document.getElementById (collapseb odies).sty le.display == 'none' && document.getElementById(co llapsehead ers).style .display == 'none'){
document.getElementById(ju stheaders) .style.dis play = 'block';
}else{
document.getElementById(ju stheaders) .style.dis play = 'none';
}
}
</script>
HTML:
<a href="javascript:;" onmousedown="toggleDiv1('c ollapsebod ies'); toggleDiv2('collapseheader s'); toggleDiv3('justheaders'); ">Toggle Div Visibility</a>
I changed the code to the following:
Java:
<script language="javascript">
function toggleDiv1(collapsebodies)
if(document.getElementById
document.getElementById(co
}else{
document.getElementById(co
}
}
function toggleDiv2(collapseheaders
if(document.getElementById
document.getElementById(co
}else{
document.getElementById(co
}
}
function toggleDiv3(justheaders){
if(document.getElementById
document.getElementById(ju
}else{
document.getElementById(ju
}
}
</script>
HTML:
<a href="javascript:;" onmousedown="toggleDiv1('c
ASKER
I also tried the visibility change, it still works in the same way. I basically want it to show justheaders when collapsebodies and collapseheaders are set to none. And then hide it when they are set to block.
This is unrelated to Java, I've suggested it be moved to a more appropriate section of the site.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That still seems to do the same thing Badotz. It's almost like it's running the first two and not the third. Maybe the code is wrong on the third? Also I have set the display to none on the third function in the CSS. So when the page loads initially you see the first two divs. When you click the link the first two disappear, but the third div doesn't seem to show after the link click.
The function below might work. Invoke it like this:
<a href="#" onmousedown="toggleDiv('[c ollapsebod ies', 'collapseheaders', ''justheaders']);">Toggle Div Visibility</a>
<a href="#" onmousedown="toggleDiv('[c
<script type="text/javascript">
function toggleDiv(ra) {
var i = 0,
elem,
mode = '';
function $(id) { return document.getElementById(id); }
for (i = 0; i < ra.length; i += 1) {
elem = $(ra[i]);
elem.style.display = (elem.style.display !== 'none') ? 'none' : 'block'; // Hide if shown
}
}
You can delete the "mode" var since it is not used (it was in a previous version, but not now).
Make sure you change the "," to a ";" after the "elem" var.
Make sure you change the "," to a ";" after the "elem" var.
ASKER
That doesn't seem to do anything when I click the link with that code. Here is what I used:
function toggleDiv(ra) {
var i = 0,
elem;
function $(id) { return document.getElementById(id ); }
for (i = 0; i < ra.length; i += 1) {
elem = $(ra[i]);
elem.style.display = (elem.style.display !== 'none') ? 'none' : 'block'; // Hide if shown
}
}
<a href="#" onmousedown="toggleDiv('[c ollapsebod ies', 'collapseheaders', ''justheaders']);">Toggle Div Visibility</a>
Also tried it with the mode, and still didn't seem to do anything :(
function toggleDiv(ra) {
var i = 0,
elem;
function $(id) { return document.getElementById(id
for (i = 0; i < ra.length; i += 1) {
elem = $(ra[i]);
elem.style.display = (elem.style.display !== 'none') ? 'none' : 'block'; // Hide if shown
}
}
<a href="#" onmousedown="toggleDiv('[c
Also tried it with the mode, and still didn't seem to do anything :(
After
elem = $(ra[i]);
add
alert(elem.id);
to make sure the function is invoked.
It might be due to the "#" href, but I can't be sure...
elem = $(ra[i]);
add
alert(elem.id);
to make sure the function is invoked.
It might be due to the "#" href, but I can't be sure...
ASKER
That still seems to be doing nothing, I tried to changing the link from "#" to "javascript:;" also, and that didn't seem to work either.
Does it have to be an <a> element? And does it have to be the "onmousedown" event? I'd use "onclick", but that's just me.
ASKER
onclick seems to do the same thing as onmousedown. Spose it doesn't have to be an <a> would just be a lot easier that's all. It all seems to working, it's only that last function that doesn't seem to work right. I might just see if I can get the last function working by itself. Then might be able to fix it.
ASKER
I got it to work!
I changed toggleDiv3 to:
function toggleDiv3(justheaders){
if(document.getElementById (justheade rs).style. display == 'block'){
document.getElementById(ju stheaders) .style.dis play = 'none';
}else{
document.getElementById(ju stheaders) .style.dis play = 'block';
}
}
from:
function toggleDiv3(justheaders){
if(document.getElementById (collapseb odies).sty le.display == 'none' && document.getElementById(co llapsehead ers).style .display == 'none'){
document.getElementById(ju stheaders) .style.dis play = 'block';
}else{
document.getElementById(ju stheaders) .style.dis play = 'none';
}
}
I'm not sure why the other one didn't work, logically it should, anyway the other way seems to work.
Thanks for all your help,
Quintin
I changed toggleDiv3 to:
function toggleDiv3(justheaders){
if(document.getElementById
document.getElementById(ju
}else{
document.getElementById(ju
}
}
from:
function toggleDiv3(justheaders){
if(document.getElementById
document.getElementById(ju
}else{
document.getElementById(ju
}
}
I'm not sure why the other one didn't work, logically it should, anyway the other way seems to work.
Thanks for all your help,
Quintin