Forum Moderators: open

Message Too Old, No Replies

Show/Hide HTML with js

         

Mr Bo Jangles

9:12 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



Hi all,
I want to add a feature in a web app that will utilize show/hide javascript.
I have found the code below, and it seems to work just fine in IE, Firefox, and Chrome:

First question: Any issues with this code? Does it look optimum?

And a supplementary question:
If a user says it isn't working for them, what is it that they are most likely to have either not installed, or switched off these days that would stop a simple script like this?

<script type="text/javascript" language="JavaScript"><!--
function RemoveContent(d) {
document.getElementById(d).style.display = "none";
}
function InsertContent(d) {
document.getElementById(d).style.display = "";
}
//--></script>

==== HTML part ====

<a href="javascript:InsertContent('div1')">
Show
</a> &nbsp; &nbsp;
<a href="javascript:RemoveContent('div1')">
Hide
</a><br>

<div id="div1" style="display: none;">
<b>This content NOT initially displayed</b>
</div>

<hr>

<a href="javascript:InsertContent('div2')">
Show
</a> &nbsp; &nbsp;

<a href="javascript:RemoveContent('div2')">
Hide
</a><br>

<div id="div2" style="display: block;">
<b> This content IS initially displayed </b>
</div>

methode

9:17 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



they have JS turned off, they use LYNX, or they use a browser which thinks that getElementById() is fried potato with mustard and will throw an error in better cases.

The most likely situation is that they have JS turned off.

jpope

6:30 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



That code should work on any modern browser. If it doesn't, it would mean javascript is disabled (by default javascript is not disabled so they would have had to changed this themselves previously) or they are using an archaic or unusual browser.