Changed the name attribute to id but not sure how to amend my JavaScript.
Hi there,
I've just changed the "name" attribute being used in a piece of working JavaScript to "id".
The script allows me to hide/show sections of text on a page...
var sections = new Array('beginners', 'higher')
function closeAll()
{
var i;
for (i=0; i<sections.length; i++) {
var obj = document.getElementById(sections[i]);
if (eval("typeof document.img"+sections[i])!='undefined') {
eval("document.img"+sections[i]+".src='images/'+'plus.png'");
obj.style.display = 'none';
}
}
}
function switchBlock(SWEET) {
var obj = document.getElementById(SWEET);
if (eval("typeof document.img"+SWEET)!='undefined') {
if(obj.style.display == 'none') {
eval("document.img"+SWEET+".src='images/'+'minus.png'");
obj.style.display = 'block';
}
else {
eval("document.img"+SWEET+".src='images/'+'plus.png'");
obj.style.display = 'none';
}
eval("document.img"+SWEET+".blur()");
}
}
function toFront(section)
{
document.all['beginners'].style.visibility='hidden';
document.all['higher'].style.visibility='hidden';
Thanks for your help - I used both changes you gave me and couldn't get it to work as it was working before.
With regard to "hideous and ancient code"...ha , yes, not surprising...since I took it from a website many a year ago - it still works so didn't bother looking to update it. However, the code on the page is not validating under "xhtml1-strict.dtd" because I'm using the "name" attribute. Having changed it to "id", the JavaScript no longer works.
As you can tell, I don't know JavaScript!
The way it worked before was that there were two sections that were hidden on a page when it opened up (the code you gave me had the sections open by default).
These sections were in the JavaScript code under....
var sections = new Array('hidden1', 'hidden2')
...and...
function toFront(section)
{
document.all['hidden1'].style.visibility='hidden';
document.all['hidden2'].style.visibility='hidden';
On the actual html page, the hidden sections were opened by either clicking the image, "plus.png" or the text next to it (I didn't refer to the text link in my initial post). This is the code for the first hidden section called "hidden1"...
<a href="JavaScript:switchBlock('hidden1')"><img src="images/plus.png" name="imghidden1" width="18" height="18" alt="Clicking This Image Opens the Hidden Section 1" /></a>
<a class="SectionLink" href="JavaScript:switchBlock('hidden1')">This Link Opens the Hidden Section 1</a>
I probably don't need to explain this to you but to any novices like me reading this...I'd like to add that it is the same link that opens the hidden text and then closes it again.
I did a little Google digging and got your better code to work the way I wanted it too...thanks. Sorry...I just needed a step by step explanation coz that's just how I am
I went with....
<script type="text/javascript">
function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="block")
which.style.display="none"
else
which.style.display="block"
}
</script>
Significantly less hideuos. Slightly less ancient. getElementById was introduced in 2000, so we can be fairly sure that most browsers can use it - there's no need to check for it, really. Also if you really want to use those a tags, calling javascript from the href is very old school. Here's an update:
ah. it should be href="#" (left out a " ). Dunno if that validates - I don't much care about html validation - but I suppose it would, it's a fairly standard approach.
Getting rid of the a tags is easy - you can even make text look like links with a little css, and just use their onclick events...
Removing the "a" tag didn't validate unfortunately but as it turns out...the whole hide/show function is being scraped - apparently it has been decided that the information is now going on to separate pages!
In any case, thanks for all your help - it's been duly noted The site where I took the initial "hideous and ancient code" is going to have a facelift - I'll be telling them what to use instead .
Bookmarks