Explanation of code needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Thompson

    #1

    Explanation of code needed

    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


  • McKirahan

    #2
    Re: Explanation of code needed

    "Phil Thompson" <philSPAM@doubl onegative.com> wrote in message
    news:423efa96$0 $10940$cc9e4d1f @news-text.dial.pipex .com...[color=blue]
    > 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[/color]
    on[color=blue]
    > the page. How can I change the script (below) so it only hides a[/color]
    particular[color=blue]
    > form, with an id or even better a class? I can well imagine that alot of[/color]
    the[color=blue]
    > 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.[/color]

    [snip]

    Without studying your code...

    Have you tried putting a <span id="like_a_pre" > before the <form> tag and
    </span> after the </form> tag and doing what was originally done -- changing
    ('pre') to ('like_a_pre')?


    Comment

    • Phil Thompson

      #3
      Re: Explanation of code needed


      "McKirahan" <News@McKirahan .com> wrote in message
      news:j9Wdnaa5tc-biKLfRVn-sQ@comcast.com. ..[color=blue]
      > "Phil Thompson" <philSPAM@doubl onegative.com> wrote in message
      > news:423efa96$0 $10940$cc9e4d1f @news-text.dial.pipex .com...[color=green]
      >> 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[/color]
      > on[color=green]
      >> the page. How can I change the script (below) so it only hides a[/color]
      > particular[color=green]
      >> form, with an id or even better a class? I can well imagine that alot of[/color]
      > the[color=green]
      >> 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.[/color]
      >
      > [snip]
      >
      > Without studying your code...
      >
      > Have you tried putting a <span id="like_a_pre" > before the <form> tag and
      > </span> after the </form> tag and doing what was originally done --
      > changing
      > ('pre') to ('like_a_pre')?
      >[/color]

      I may be wrong, but, I think that would create invalid HTML. I have tried
      a similar solution by making it a div with and id and changing it from
      getElementsByTa gName('pre') to getElementById( 'like_a_pre') to no avail


      Comment

      • RobG

        #4
        Re: Explanation of code needed

        Phil Thompson wrote:[color=blue]
        > 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[/color]

        You can get all the forms in a page using the forms collection:

        var allTheForms = document.forms;
        [color=blue]
        > 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[/color]

        You can selectively hide using getElementById, or you can give
        each form a class and use that. The class does not need to be
        defined anywhere and you can have multiple classes on an
        element to create a kind of group.

        e.g. <form action="" class="type1 type2 type3" ... >

        Then your script could be:

        function hideForm( classToHide ){
        var f = document.forms,
        i = f.length,
        t,
        c = new RegExp('\\b' + classToHide + '\\b');

        while (i--) {
        t = f[i];
        if ( t.style && c.test(t.classN ame) ) {
        t.style.display = 'none';
        } else {
        t.style.display = '';
        }
        }
        }


        and you could hide the above table with:

        ... onclick="hideFo rm('type2');" ...

        to show all forms, just pass a 'classname' that doesn't exist:

        ... onclick="hideFo rm('xx');" ...



        then any form with a class of 'type2' will be hidden and any
        others will be shown.
        [color=blue]
        > 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.
        >[/color]

        The following is an example of what you are trying to do:

        <script type="text/javascript">
        function hideForm( classToHide ){
        var f = document.forms,
        i = f.length,
        t,
        c = new RegExp('\\b' + classToHide + '\\b');

        while (i--) {
        t = f[i];
        if ( t.style && c.test(t.classN ame) ) {
        t.style.display = 'none';
        } else {
        t.style.display = '';
        }
        }
        }

        </script>

        <form action="" class="forma">
        <input type="text" value="forma">
        </form>
        <form action="" class="forma formb">
        <input type="text" value="forma formb">
        </form>
        <form action="" class="formc">
        <input type="text" value="formc">
        </form>
        <form action="" class="formb">
        <input type="text" value="formb">
        </form>
        <form action="" class="forma formc">
        <input type="text" value="forma formc">
        </form>

        <form action="">
        <input type="radio" name="zz" onclick="hideFo rm('forma');">
        hide forma<br>
        <input type="radio" name="zz" onclick="hideFo rm('formb');">
        hide formb<br>
        <input type="radio" name="zz" onclick="hideFo rm('formc');">
        hide formc<br>
        <input type="radio" name="zz" onclick="hideFo rm('x');">
        hide none<br>
        </form>

        [...]


        --
        Rob

        Comment

        • Phil Thompson

          #5
          Re: Explanation of code needed


          "RobG" <rgqld@iinet.ne t.auau> wrote in message
          news:3DM%d.474$ Zn.27412@news.o ptus.net.au...[color=blue]
          > Phil Thompson wrote:[color=green]
          >> 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[/color]
          >
          > You can get all the forms in a page using the forms collection:
          >
          > var allTheForms = document.forms;
          >[color=green]
          >> 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[/color]
          >
          > You can selectively hide using getElementById, or you can give
          > each form a class and use that. The class does not need to be
          > defined anywhere and you can have multiple classes on an
          > element to create a kind of group.
          >
          > e.g. <form action="" class="type1 type2 type3" ... >
          >
          > Then your script could be:
          >[/color]
          <snip />[color=blue]
          > --
          > Rob[/color]

          Thanks RobG that's really helpful.

          Phil


          Comment

          Working...