P
Phil Thompson
Hi
I'm very new to JavaScript and just need a bit of an explanation to some
code and some ideas of how to edit it to do what I want.
The script looks for every <pre> on a page and sets them to
style="display:none" it then produces links to show and hide the hidden
<pre>s. My problem is that I don't wish it to be a <pre> therefore I have
just change the line getElementsByTagName('pre') to
getElementsByTagName('form') to which is what I want to hide. But, this
doens't resolve my problem of the script hiding every single form element on
the page. How can I change the script (below) so it only hides a particular
form, with an id or even better a class? I can well imagine that alot of the
script is useless to me as I only want to hide a specific id or class and
not all of instances tag. Trouble is I don't really understand the script
that well.
p.s. I have tried to do the following;
remove getElementsByTagName('form')
and replace it with getElementByID('form_id_here')
to no avail
script (courtesy of onlinetools.org
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter1.html)...
function collapsepre()
{
var pres,i,allp,alla,head,newa,newp;
pres=document.getElementsByTagName('form');
if(pres.length>0)
{
allp=document.createElement('p');
alla=document.createElement('a');
alla.appendChild(document.createTextNode('Expand all Examples'));
alla.onclick=function(){col('all',this);return false;};
alla.setAttribute('href','#');
alla.className='collink';
allp.appendChild(alla);
head=document.getElementsByTagName('h1')[0];
head.parentNode.insertBefore(allp,head.nextSibling)
}
for(i=0;i<pres.length;i++)
{
newa=document.createElement('a');
newp=document.createElement('p');
newa.appendChild(document.createTextNode('Post Message'));
newa.tocollapse=i;
newa.onclick=function(){col(this,this.tocollapse);return false;};
newa.setAttribute('href','#');
newa.className='collink';
newp.appendChild(newa);
newp.style.marginBottom='5px';
newp.style.marginLeft='10px';
pres.style.marginTop=0;
pres.style.display='none';
pres.parentNode.insertBefore(newp,pres);
}
}
function col(a,o){
var pres,i,n;
switch(a){
case 'all':
pres=document.getElementsByTagName('form');
for(i=0;i<pres.length;i++)
{
pres.style.display='block';
pres.previousSibling.style.display='none';
}
o.onclick=function(){col('colall',this);return false;};
o.firstChild.nodeValue='Collapse all Examples';
break;
case 'colall':
pres=document.getElementsByTagName('form');
for(i=0;i<pres.length;i++)
{
pres.style.display='none';
pres.previousSibling.style.display='block';
}
o.onclick=function(){col('all',this);return false;};
o.firstChild.nodeValue='Expand all Examples';
break;
default:
n=document.getElementsByTagName('form')[o];
n.style.display=n.style.display=='block'?'none':'block';
a.firstChild.nodeValue=n.style.display=='block'?'Don\'t Post':'Post
message';
break;
}
}
if(document.getElementById && document.createTextNode)
{
window.onload=function(){
collapsepre();
}
}
Phil
I'm very new to JavaScript and just need a bit of an explanation to some
code and some ideas of how to edit it to do what I want.
The script looks for every <pre> on a page and sets them to
style="display:none" it then produces links to show and hide the hidden
<pre>s. My problem is that I don't wish it to be a <pre> therefore I have
just change the line getElementsByTagName('pre') to
getElementsByTagName('form') to which is what I want to hide. But, this
doens't resolve my problem of the script hiding every single form element on
the page. How can I change the script (below) so it only hides a particular
form, with an id or even better a class? I can well imagine that alot of the
script is useless to me as I only want to hide a specific id or class and
not all of instances tag. Trouble is I don't really understand the script
that well.
p.s. I have tried to do the following;
remove getElementsByTagName('form')
and replace it with getElementByID('form_id_here')
to no avail
script (courtesy of onlinetools.org
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter1.html)...
function collapsepre()
{
var pres,i,allp,alla,head,newa,newp;
pres=document.getElementsByTagName('form');
if(pres.length>0)
{
allp=document.createElement('p');
alla=document.createElement('a');
alla.appendChild(document.createTextNode('Expand all Examples'));
alla.onclick=function(){col('all',this);return false;};
alla.setAttribute('href','#');
alla.className='collink';
allp.appendChild(alla);
head=document.getElementsByTagName('h1')[0];
head.parentNode.insertBefore(allp,head.nextSibling)
}
for(i=0;i<pres.length;i++)
{
newa=document.createElement('a');
newp=document.createElement('p');
newa.appendChild(document.createTextNode('Post Message'));
newa.tocollapse=i;
newa.onclick=function(){col(this,this.tocollapse);return false;};
newa.setAttribute('href','#');
newa.className='collink';
newp.appendChild(newa);
newp.style.marginBottom='5px';
newp.style.marginLeft='10px';
pres.style.marginTop=0;
pres.style.display='none';
pres.parentNode.insertBefore(newp,pres);
}
}
function col(a,o){
var pres,i,n;
switch(a){
case 'all':
pres=document.getElementsByTagName('form');
for(i=0;i<pres.length;i++)
{
pres.style.display='block';
pres.previousSibling.style.display='none';
}
o.onclick=function(){col('colall',this);return false;};
o.firstChild.nodeValue='Collapse all Examples';
break;
case 'colall':
pres=document.getElementsByTagName('form');
for(i=0;i<pres.length;i++)
{
pres.style.display='none';
pres.previousSibling.style.display='block';
}
o.onclick=function(){col('all',this);return false;};
o.firstChild.nodeValue='Expand all Examples';
break;
default:
n=document.getElementsByTagName('form')[o];
n.style.display=n.style.display=='block'?'none':'block';
a.firstChild.nodeValue=n.style.display=='block'?'Don\'t Post':'Post
message';
break;
}
}
if(document.getElementById && document.createTextNode)
{
window.onload=function(){
collapsepre();
}
}
Phil