<$BlogRSDUrl$>


Loser's Guide Loser's Guide

 Loser's Guide to Life

Friday, July 28, 2006

Javascript Time 

It took a while to figure this out. What was needed was a way of switching between blocks of text so that each would cancel the other out. The “toggle” script does the first thing: it turns a block of text (a “div”) on or off. But I wanted each one to turn off the other when it came on, and it seems you can only turn the thing itself on or off. There seems to be no way to adjust or add to the toggle script, so you have to make a whole extra script that will turn things off, called “closeAll” (that's just a handy name for it).

Where the first script looks for elements by their “id” and fires them, the second one has to look for any “divs” that have “display:none” in their style. But it has to locate them by part of their “id”, so you have to specify a prefix in the script and then put that prefix on the “id” of each “div” that you want to be affected. Hmmm.


[toggle script]<script type="text/javascript">
<!--
function toggle(obj) {
if (document.getElementById(obj).style.display == 'none'){
document.getElementById(obj).style.display = 'block';
}
else {
document.getElementById(obj).style.display = 'none';
}
}
-->
</script>

[puff, puff, and the script to turn them off:]

<script type="text/javascript">
<!--
function closeAll(){
var d = document.getElementsByTagName('div');
for(var i=0;i<d.length;i++){
if(d[i].id.indexOf('playersFilter')>-1){
d[i].style.display='none';
}
}
}
-->
</script>

where each “div” has the prefix 'playersFilter' in its “id”, as: 'playersFilter[Name]' (and the [Name] bit is different for each one, for example 'playersFilterParagraph1', etc.)

Well, all that is pretty straightforward. It is childishly simple. But how do you make it do stuff?! That's a bit of a puzzle, because why would it close anything? You've just clicked it on, stupid.

The trick is to “call the function”, as they say, via an Event! In this case the function is “closeAll” and the event is “onBlur”, which tells what is to happen after you've clicked on something:

<a href="javascript:toggle('playersFilterParagraph1')" onBlur="closeAll()">Paragraph One</a>

And so there you have it. That's how it's done. I don't say anyone needs it though.



0 Comments:

Post a Comment


Watching TV is a good way to tear yourself away from the computer.