/    Sign up×
Community /Pin to ProfileBookmark

Toggle Script – first click does not work

Hi
Strange problem…I am using a small script to toggle the background colour of a <div> when it is clicked – to “highlight” that it has been selected.

Clicking this div also reveals a hidden div below it – containing the sublinks of that section, and hides any other hidden divs that may be revealed at that moment.

My code is below and it is kind of working. All the showing and hiding of hidden divs works fine.

The problem is with the toggle background color – the very first click does not work, but it does on the second and subsequent clicks, but this puts everything out of sync – it “highlights” the “title” when it should be removing the “highlight”. Any suggestions as to why?

Finally, if this looks a right mess to you masters, please have patience as this is my very first venture into scripting!

Thanks

[CODE]SCRIPTS IN HEAD:
<script language=’javascript’ type=’text/javascript’>
function toggleBgColor(divid){
if (document.getElementById(divid).style.backgroundColor == ‘transparent’){
document.getElementById(divid).style.backgroundColor =’#3a3a46′;
}else{
document.getElementById(divid).style.backgroundColor =’transparent’;
}
}
</script>
<script language=’javascript’ type=’text/javascript’>
function toggleDiv(divid){
if(document.getElementById(divid).style.display == ‘none’){
document.getElementById(divid).style.display = ‘block’;
}else{
document.getElementById(divid).style.display = ‘none’;
}
}
</script>[/CODE]

[code=html]HTML:

<div id=”essays” onClick=”javascript:toggleBgColor(‘essays’);”><a href=”#” onclick=”toggleDiv(‘hidden1’); document.getElementById(‘hidden2′).style.display=’none’; document.getElementById(‘hidden3′).style.display=’none’; document.getElementById(‘hidden4′).style.display=’none’; document.getElementById(‘multi’).style.backgroundColor=’transparent’; document.getElementById(‘india’).style.backgroundColor=’transparent’; document.getElementById(‘travel’).style.backgroundColor=’transparent’; return false;”>Essays</a></div>
<div style=”display:none; padding-bottom: 5px; border-bottom: 1px solid #3a3a46; width:170px; ” id=”hidden1″>
<div class=”link”>»&nbsp;<a href=”/site/photoessays/homeless/index.php”>Homeless in Southern &nbsp;&nbsp;&nbsp;India</a></div>
<div class=”link”>»&nbsp;<a href=”/site/photoessays/shiva/index.php”>Shiva</a></div>
</div>

CSS:

#essays {
width: 170px;
background-color: transparent;
}

html>body #essays {
width: auto;
min-width: 170px;
}

#essays a {
display: block;
color: #ffffff;
background-color: transparent;
font-size: 12px;
font-weight: bold;
padding: 3px 1px 3px 4px
margin-bottom: 5px;
margin-top: 5px;
border-bottom: 1px solid #3a3a46;
}

#essays a:hover {
background-color: #3a3a46;
}[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisApr 24.2007 — This is usually an indication that the "if" statement is not finding what you think is there. Make sure that the initial value of backgroundColor is actually "transparent". It most probably is not, since it skips to the wrong step on the initial click, but once it is actually set to "transparent" it works as expected.
Copy linkTweet thisAlerts:
@trebor31authorApr 24.2007 — Thanks very much - really helped as I did not even know what area could be wrong - I thought I may have written the script badly.

The CSS in the stylesheet did specify the correct background colour as transparent - so dont know why it didnt work in the first place.

However got it to work by adding - style="background-color: transparent;" - to the div tag in the html.

Learning something every day!

Thanks
×

Success!

Help @trebor31 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...