/    Sign up×
Community /Pin to ProfileBookmark

show/hide Toggle not working.

I toggle div tags with this function:

[code]
<script type=”text/javascript”>
function Toggle(which){
if (!document.getElementById)
return
if (which.style.display==”block”)
which.style.display=”none”
else
which.style.display=”block”
}

function Switch(CarID){
for (var i = 1; i<=7; i++){
Toggle(document.getElementById(i+’Show’+CarID));
Toggle(document.getElementById(i+’Edit’+CarID));
}
}

</script>
[/code]

the html :

[code]
<table>

<tr><td valign=”top” ><div id=”1Show1″ align=”left”> Camaro<!–Model –></div><div id=”1Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div id=”2Show1″ align=”left”>Chevrolet<!–Make –></div><div id=”2Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div id=”3Show1″ align=”left”>Car<!–Type –></div><div id=”3Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div id=”4Show1″ align=”left”>1999<!–Year –></div><div id=”4Edit1″ style=”display: none”>Now you see me</div></td></tr>

<tr><td valign=”top” ><div id=”5Show1″ align=”left”>75000<!–Mileage –></div><div id=”5Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div id=”6Show1″ align=”left”>4000<!–Price –></div><div id=”6Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div id=”7Show1″ align=”left”>Used<!–Car lot –></div><div id=”7Edit1″ style=”display: none”>Now you see me</div></td></tr>
<tr><td valign=”top” ><div align=”right”><a href=”javascript: Switch(1);”>Edit</a></div></td></tr>
</table></td></tr></table>
[/code]

The problem is that the first time I click on ‘Edit’ the hidden <div>’s appear and the showing <div>’s dont disapear. The next time I click it, they both appear, and after another click both disapear.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemDec 10.2006 — 
The problem is that the first time I click on 'Edit' the hidden <div>'s appear and the showing <div>'s dont disapear. The next time I click it, they both appear, and after another click both disapear.[/QUOTE]
The showing elements are unstyled, and therefore have no display value set.

Either change your code to
[CODE]if (which.style.display=="block" || which.style.display=="")
which.style.display="none"
else
which.style.display="block"
[/CODE]
or test for 'none' first.
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — Sorry to dumpster dive, but I am having this same problem!!! What do you mean by "or test for 'none' first?"
Copy linkTweet thisAlerts:
@spangltkauthorMar 20.2007 — The div tags are in the code, defining what code will toggle. However the div tags are missing style='display:block' or style='display:none'. He saying that the function needs to first check if the style tag even exists in the div tag.
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — I resolved this problem by putting style="display:none" into the tag LOCALLY. However, it was already being specified in my linked style sheet... Not sure why, but it's a lot smoother with the local specification of the style. Anyone know why that is? Weird.
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — Maybe it takes a few seconds for the page to call the linked style sheet?
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — Here is the Javascript I'm using:

&lt;script language="javascript" type="text/javascript"&gt;
function toggle(elementID){
var target1 = document.getElementById(elementID)
if (target1.style.display == 'none') {
target1.style.display = 'block'
} else {
target1.style.display = 'none'
}
}
&lt;/script&gt;


And here is the CSS:

#collections {
margin: 0 0 0 10px;
display: none;
}


&lt;p&gt;
&lt;a href="javascript:toggle('collections');"&gt;Show/Hide&lt;/a&gt;&lt;br /&gt;
&lt;div id="collections" style="display:none"&gt;
&lt;a href="/site/interests/collections/books"&gt;Books&lt;/a&gt;&lt;br /&gt;
&lt;a href="/site/interests/collections/magazines"&gt;Magazines&lt;/a&gt;&lt;br /&gt;
&lt;a href="/site/interests/collections/movies"&gt;Movies&lt;/a&gt;&lt;br /&gt;
&lt;a href="/site/interests/collections/music"&gt;Music&lt;/a&gt;&lt;br /&gt;
&lt;a href="/site/interests/collections/video_games"&gt;Video Games&lt;/a&gt;&lt;br /&gt;
&lt;a href="/site/interests/collections/wristwatches"&gt;Wristwatches&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;/p&gt;


Doesn't make sense, since I'm calling [b]style="display:none"[/b] two times (once externally and once locally). If I only call it from the linked style sheet, it requires two clicks to toggle. If I call it locally, it only requires one click to toggle...
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — Anybody have any idea here? As far as I know, this should be working whether the CSS is local or not.
Copy linkTweet thisAlerts:
@ryryjsMar 20.2007 — Could also use this script instead...

<i>
</i>&lt;script language="javascript" type="text/javascript"&gt;
function toggle(obj) {
var el = document.getElementById(obj);
if ( el.style.display != 'none' ) {
el.style.display = 'none';
}
else {
el.style.display = '';
}
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@ryryjsMar 21.2007 — Anybody have any idea here? As far as I know, this should be working whether the CSS is local or not.[/QUOTE]

Anybody?!
×

Success!

Help @spangltk 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...