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 getElementsByTa gName('pre') to
getElementsByTa gName('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 getElementsByTa gName('form')
and replace it with getElementByID( 'form_id_here')
to no avail
script (courtesy of onlinetools.org
http://www.onlinetools.org/articles/...chapter1.html)...
function collapsepre()
{
var pres,i,allp,all a,head,newa,new p;
pres=document.g etElementsByTag Name('form');
if(pres.length> 0)
{
allp=document.c reateElement('p ');
alla=document.c reateElement('a ');
alla.appendChil d(document.crea teTextNode('Exp and all Examples'));
alla.onclick=fu nction(){col('a ll',this);retur n false;};
alla.setAttribu te('href','#');
alla.className= 'collink';
allp.appendChil d(alla);
head=document.g etElementsByTag Name('h1')[0];
head.parentNode .insertBefore(a llp,head.nextSi bling)
}
for(i=0;i<pres. length;i++)
{
newa=document.c reateElement('a ');
newp=document.c reateElement('p ');
newa.appendChil d(document.crea teTextNode('Pos t Message'));
newa.tocollapse =i;
newa.onclick=fu nction(){col(th is,this.tocolla pse);return false;};
newa.setAttribu te('href','#');
newa.className= 'collink';
newp.appendChil d(newa);
newp.style.marg inBottom='5px';
newp.style.marg inLeft='10px';
pres[i].style.marginTo p=0;
pres[i].style.display= 'none';
pres[i].parentNode.ins ertBefore(newp, pres[i]);
}
}
function col(a,o){
var pres,i,n;
switch(a){
case 'all':
pres=document.g etElementsByTag Name('form');
for(i=0;i<pres. length;i++)
{
pres[i].style.display= 'block';
pres[i].previousSiblin g.style.display ='none';
}
o.onclick=funct ion(){col('cola ll',this);retur n false;};
o.firstChild.no deValue='Collap se all Examples';
break;
case 'colall':
pres=document.g etElementsByTag Name('form');
for(i=0;i<pres. length;i++)
{
pres[i].style.display= 'none';
pres[i].previousSiblin g.style.display ='block';
}
o.onclick=funct ion(){col('all' ,this);return false;};
o.firstChild.no deValue='Expand all Examples';
break;
default:
n=document.getE lementsByTagNam e('form')[o];
n.style.display =n.style.displa y=='block'?'non e':'block';
a.firstChild.no deValue=n.style .display=='bloc k'?'Don\'t Post':'Post
message';
break;
}
}
if(document.get ElementById && document.create TextNode)
{
window.onload=f unction(){
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 getElementsByTa gName('pre') to
getElementsByTa gName('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 getElementsByTa gName('form')
and replace it with getElementByID( 'form_id_here')
to no avail
script (courtesy of onlinetools.org
http://www.onlinetools.org/articles/...chapter1.html)...
function collapsepre()
{
var pres,i,allp,all a,head,newa,new p;
pres=document.g etElementsByTag Name('form');
if(pres.length> 0)
{
allp=document.c reateElement('p ');
alla=document.c reateElement('a ');
alla.appendChil d(document.crea teTextNode('Exp and all Examples'));
alla.onclick=fu nction(){col('a ll',this);retur n false;};
alla.setAttribu te('href','#');
alla.className= 'collink';
allp.appendChil d(alla);
head=document.g etElementsByTag Name('h1')[0];
head.parentNode .insertBefore(a llp,head.nextSi bling)
}
for(i=0;i<pres. length;i++)
{
newa=document.c reateElement('a ');
newp=document.c reateElement('p ');
newa.appendChil d(document.crea teTextNode('Pos t Message'));
newa.tocollapse =i;
newa.onclick=fu nction(){col(th is,this.tocolla pse);return false;};
newa.setAttribu te('href','#');
newa.className= 'collink';
newp.appendChil d(newa);
newp.style.marg inBottom='5px';
newp.style.marg inLeft='10px';
pres[i].style.marginTo p=0;
pres[i].style.display= 'none';
pres[i].parentNode.ins ertBefore(newp, pres[i]);
}
}
function col(a,o){
var pres,i,n;
switch(a){
case 'all':
pres=document.g etElementsByTag Name('form');
for(i=0;i<pres. length;i++)
{
pres[i].style.display= 'block';
pres[i].previousSiblin g.style.display ='none';
}
o.onclick=funct ion(){col('cola ll',this);retur n false;};
o.firstChild.no deValue='Collap se all Examples';
break;
case 'colall':
pres=document.g etElementsByTag Name('form');
for(i=0;i<pres. length;i++)
{
pres[i].style.display= 'none';
pres[i].previousSiblin g.style.display ='block';
}
o.onclick=funct ion(){col('all' ,this);return false;};
o.firstChild.no deValue='Expand all Examples';
break;
default:
n=document.getE lementsByTagNam e('form')[o];
n.style.display =n.style.displa y=='block'?'non e':'block';
a.firstChild.no deValue=n.style .display=='bloc k'?'Don\'t Post':'Post
message';
break;
}
}
if(document.get ElementById && document.create TextNode)
{
window.onload=f unction(){
collapsepre();
}
}
Phil
Comment