Loading ...
Sorry, an error occurred while loading the content.

Re: DOM Scripting by Jeremy Keith

Expand Messages
  • evzen.pomschar
    Hi Jon, thank you for your reply. I understand all the code until it hits the var odd = false. How does that triggers the rest of the code? I see it as that
    Message 1 of 7 , Jul 18, 2009
    • 0 Attachment
      Hi Jon,

      thank you for your reply.
      I understand all the code until it hits the var odd = false.
      How does that triggers the rest of the code? I see it as that this variable (odd)is not related to the var = rows. If it is not related to the var = rows how does the var rows knows that the odd <tr> shold be changed?

      Does it make sence?

      Thanks Ev.

      --- In JavaScript_Official@yahoogroups.com, Jon Stephens <jon@...> wrote:
      >
      >
      > > DOM Scripting by Jeremy Keith
      > > Posted by: "evzen.pomschar" rokker@... evzen.pomschar
      > > Date: Fri Jul 17, 2009 4:20 pm ((PDT))
      > >
      > >
      > > I am a novice in Javascript and right now going through a book by Jeremy
      > > Keith - "DOM Scripting"
      > >
      > > I would be pleased if someone could explain me one function:
      >
      > See my comments added to the code below. Does this help?
      >
      > ...
      >
      > var tables = document.getElementsByTagName('table');
      > // tables = set of all <table> elements in this page
      >
      > for(var i=0; i<tables.length; i++) {
      > // for each element in tables...
      >
      > var odd = false;
      >
      > var rows = tables[i].getElementsByTagName('tr');
      > // rows = set of all <tr> elements in the i-th table
      >
      > for (var j=0; j<rows.length; j++) {
      > // for each element in rows...
      >
      > if (odd == true) {
      >
      > rows[j].style.backgroundColor = "#ffc"
      > // set this <tr>'s background colour to #FFC
      >
      > odd = false;
      >
      > } else {
      >
      > odd = true;
      >
      > }
      >
      > ...
      >
      > cheers
      >
      > jon.
      > --
      > This message has not been scanned for viruses.
      >
      > Since I do not use a Microsoft operating
      > system or software, and use only plaintext
      > for email, there is little need for me to do so.
      >
    • evzen.pomschar
      Hi Dave, Thank you for your reply. So the var odd is a part of the loop right? At first the var rows dosn t trigger because the var odd is false. Whan script
      Message 2 of 7 , Jul 18, 2009
      • 0 Attachment
        Hi Dave,

        Thank you for your reply.

        So the var odd is a part of the loop right? At first the var rows dosn't trigger because the var odd is false. Whan script gets to the end of the function var odd changes to true so the next loop can trigger <tr>, right?

        Thank you kindly once more.

        Best Regards

        Ev

        --- In JavaScript_Official@yahoogroups.com, "David Smart" <smartware.consulting@...> wrote:
        >
        > "Odd" is the thing that determines odd/even rows so that it can stripe
        > alternate rows. It is initially false, then is tested in the if statement
        > to see if the row should be striped or not. If it's true, the row gets
        > striped and "odd" is set to false; if it's false, it's set to true and the
        > row is not striped.
        >
        > IMHO this is not particularly good code to use as a learning example ...
        >
        > Firstly, the if statement is
        >
        > > if (odd == true)
        >
        > which is unnecessary. It should simply be
        >
        > > if (odd)
        >
        > which is not only shorter, but is actually more readable.
        >
        > Secondly, the variable is called "odd" but is not really used in accordance
        > with its name. First time through should be the odd time, and second time
        > through should be the even time, but it doesn't work that way, it's the
        > opposite.
        >
        > It might be said that it is looking to see if "j" is odd, but that would
        > normally be done by a simple
        >
        > > if ((j % 2) == 1)
        >
        > which does indeed test to see if "j" is odd, and removes any need for a
        > variable - thereby removing 5 lines from the code.
        >
        > Lastly, the function has a "return false" if the browser doesn't support
        > getElementsByTagName, but no corresponding "return true" when it has worked.
        > Presumably it should just have had a "return".
        >
        > Regards, Dave S
        >
        > ----- Original Message -----
        > From: "evzen.pomschar" <rokker@...>
        > To: <JavaScript_Official@yahoogroups.com>
        > Sent: Friday, July 17, 2009 7:18 AM
        > Subject: [JavaScript] DOM Scripting by Jeremy Keith
        >
        >
        > >
        > > I am a novice in Javascript and right now going through a book by Jeremy
        > > Keith - "DOM Scripting"
        > >
        > > I would be pleased if someone could explain me one function:
        > >
        > >
        > >
        > >
        > > function stripeTables() {
        > >
        > > if(!document.getElementsByTagName) return false;
        > >
        > > var tables = document.getElementsByTagName('table');
        > >
        > > for(var i=0; i<tables.length; i++) {
        > >
        > > var odd = false;
        > >
        > > var rows = tables[i].getElementsByTagName('tr');
        > >
        > > for (var j=0; j<rows.length; j++) {
        > >
        > > if (odd == true) {
        > >
        > > rows[j].style.backgroundColor = "#ffc"
        > >
        > > odd = false;
        > >
        > > } else {
        > >
        > > odd = true;
        > >
        > > }
        > >
        > > }
        > >
        > > }
        > >
        > > }
        > >
        > >
        > >
        > >
        > > The function stripe rows in a table.
        > >
        > > I understand the rest of the syntax but cannot see any connection of var
        > > odd with the function. How does the var odd connect to <tr>? There is no
        > > visible path for var odd to <tr>. Is the var odd part of the loop?
        > > Forgive me but I just don't see it.
        > >
        > >
        > >
        > >
        > >
        > > I would appreciate any explanation from anyone.
        > >
        > >
        > >
        > >
        > > Thanks
        > >
        > >
        > >
        > >
        > > Ev.
        > >
        > >
        > >
        > > [Non-text portions of this message have been removed]
        > >
        > >
        > >
        > > ------------------------------------
        > >
        > > Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
        > >
        > >
        > >
        >
        >
        > --------------------------------------------------------------------------------
        >
        >
        >
        > No virus found in this incoming message.
        > Checked by AVG - www.avg.com
        > Version: 8.5.375 / Virus Database: 270.13.19/2244 - Release Date: 07/17/09
        > 18:00:00
        >
      • David Smart
        That s right. However, avoid the term trigger . Here, we re just talking about the flow through the if statement in the inner loop. Trigger is normally
        Message 3 of 7 , Jul 18, 2009
        • 0 Attachment
          That's right.

          However, avoid the term "trigger". Here, we're just talking about the flow
          through the if statement in the inner loop.

          "Trigger" is normally reserved for events that occur and cause a special
          event handler function to be called.

          Regards, Dave S

          ----- Original Message -----
          From: "evzen.pomschar" <rokker@...>
          To: <JavaScript_Official@yahoogroups.com>
          Sent: Sunday, July 19, 2009 10:23 AM
          Subject: Re: [JavaScript] DOM Scripting by Jeremy Keith


          > Hi Dave,
          >
          > Thank you for your reply.
          >
          > So the var odd is a part of the loop right? At first the var rows dosn't
          > trigger because the var odd is false. Whan script gets to the end of the
          > function var odd changes to true so the next loop can trigger <tr>, right?
          >
          > Thank you kindly once more.
          >
          > Best Regards
          >
          > Ev
          >
          > --- In JavaScript_Official@yahoogroups.com, "David Smart"
          > <smartware.consulting@...> wrote:
          >>
          >> "Odd" is the thing that determines odd/even rows so that it can stripe
          >> alternate rows. It is initially false, then is tested in the if
          >> statement
          >> to see if the row should be striped or not. If it's true, the row gets
          >> striped and "odd" is set to false; if it's false, it's set to true and
          >> the
          >> row is not striped.
          >>
          >> IMHO this is not particularly good code to use as a learning example ...
          >>
          >> Firstly, the if statement is
          >>
          >> > if (odd == true)
          >>
          >> which is unnecessary. It should simply be
          >>
          >> > if (odd)
          >>
          >> which is not only shorter, but is actually more readable.
          >>
          >> Secondly, the variable is called "odd" but is not really used in
          >> accordance
          >> with its name. First time through should be the odd time, and second
          >> time
          >> through should be the even time, but it doesn't work that way, it's the
          >> opposite.
          >>
          >> It might be said that it is looking to see if "j" is odd, but that would
          >> normally be done by a simple
          >>
          >> > if ((j % 2) == 1)
          >>
          >> which does indeed test to see if "j" is odd, and removes any need for a
          >> variable - thereby removing 5 lines from the code.
          >>
          >> Lastly, the function has a "return false" if the browser doesn't support
          >> getElementsByTagName, but no corresponding "return true" when it has
          >> worked.
          >> Presumably it should just have had a "return".
          >>
          >> Regards, Dave S
          >>
          >> ----- Original Message -----
          >> From: "evzen.pomschar" <rokker@...>
          >> To: <JavaScript_Official@yahoogroups.com>
          >> Sent: Friday, July 17, 2009 7:18 AM
          >> Subject: [JavaScript] DOM Scripting by Jeremy Keith
          >>
          >>
          >> >
          >> > I am a novice in Javascript and right now going through a book by
          >> > Jeremy
          >> > Keith - "DOM Scripting"
          >> >
          >> > I would be pleased if someone could explain me one function:
          >> >
          >> >
          >> >
          >> >
          >> > function stripeTables() {
          >> >
          >> > if(!document.getElementsByTagName) return false;
          >> >
          >> > var tables = document.getElementsByTagName('table');
          >> >
          >> > for(var i=0; i<tables.length; i++) {
          >> >
          >> > var odd = false;
          >> >
          >> > var rows = tables[i].getElementsByTagName('tr');
          >> >
          >> > for (var j=0; j<rows.length; j++) {
          >> >
          >> > if (odd == true) {
          >> >
          >> > rows[j].style.backgroundColor = "#ffc"
          >> >
          >> > odd = false;
          >> >
          >> > } else {
          >> >
          >> > odd = true;
          >> >
          >> > }
          >> >
          >> > }
          >> >
          >> > }
          >> >
          >> > }
          >> >
          >> >
          >> >
          >> >
          >> > The function stripe rows in a table.
          >> >
          >> > I understand the rest of the syntax but cannot see any connection of
          >> > var
          >> > odd with the function. How does the var odd connect to <tr>? There is
          >> > no
          >> > visible path for var odd to <tr>. Is the var odd part of the loop?
          >> > Forgive me but I just don't see it.
          >> >
          >> >
          >> >
          >> >
          >> >
          >> > I would appreciate any explanation from anyone.
          >> >
          >> >
          >> >
          >> >
          >> > Thanks
          >> >
          >> >
          >> >
          >> >
          >> > Ev.
          >> >
          >> >
          >> >
          >> > [Non-text portions of this message have been removed]
          >> >
          >> >
          >> >
          >> > ------------------------------------
          >> >
          >> > Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
          >> >
          >> >
          >> >
          >>
          >>
          >> --------------------------------------------------------------------------------
          >>
          >>
          >>
          >> No virus found in this incoming message.
          >> Checked by AVG - www.avg.com
          >> Version: 8.5.375 / Virus Database: 270.13.19/2244 - Release Date:
          >> 07/17/09
          >> 18:00:00
          >>
          >
          >
          >
          >
          > ------------------------------------
          >
          > Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
          >
          >
          >


          --------------------------------------------------------------------------------



          Internal Virus Database is out of date.
          Checked by AVG - www.avg.com
          Version: 8.5.375 / Virus Database: 270.13.11/2232 - Release Date: 07/11/09
          17:56:00
        • evzen.pomschar
          Thanks Dave, you ve been very helpful. Regards, Ev
          Message 4 of 7 , Jul 20, 2009
          • 0 Attachment
            Thanks Dave,

            you've been very helpful.

            Regards, Ev


            --- In JavaScript_Official@yahoogroups.com, "David Smart" <smartware.consulting@...> wrote:
            >
            > That's right.
            >
            > However, avoid the term "trigger". Here, we're just talking about the flow
            > through the if statement in the inner loop.
            >
            > "Trigger" is normally reserved for events that occur and cause a special
            > event handler function to be called.
            >
            > Regards, Dave S
            >
            > ----- Original Message -----
            > From: "evzen.pomschar" <rokker@...>
            > To: <JavaScript_Official@yahoogroups.com>
            > Sent: Sunday, July 19, 2009 10:23 AM
            > Subject: Re: [JavaScript] DOM Scripting by Jeremy Keith
            >
            >
            > > Hi Dave,
            > >
            > > Thank you for your reply.
            > >
            > > So the var odd is a part of the loop right? At first the var rows dosn't
            > > trigger because the var odd is false. Whan script gets to the end of the
            > > function var odd changes to true so the next loop can trigger <tr>, right?
            > >
            > > Thank you kindly once more.
            > >
            > > Best Regards
            > >
            > > Ev
            > >
            > > --- In JavaScript_Official@yahoogroups.com, "David Smart"
            > > <smartware.consulting@> wrote:
            > >>
            > >> "Odd" is the thing that determines odd/even rows so that it can stripe
            > >> alternate rows. It is initially false, then is tested in the if
            > >> statement
            > >> to see if the row should be striped or not. If it's true, the row gets
            > >> striped and "odd" is set to false; if it's false, it's set to true and
            > >> the
            > >> row is not striped.
            > >>
            > >> IMHO this is not particularly good code to use as a learning example ...
            > >>
            > >> Firstly, the if statement is
            > >>
            > >> > if (odd == true)
            > >>
            > >> which is unnecessary. It should simply be
            > >>
            > >> > if (odd)
            > >>
            > >> which is not only shorter, but is actually more readable.
            > >>
            > >> Secondly, the variable is called "odd" but is not really used in
            > >> accordance
            > >> with its name. First time through should be the odd time, and second
            > >> time
            > >> through should be the even time, but it doesn't work that way, it's the
            > >> opposite.
            > >>
            > >> It might be said that it is looking to see if "j" is odd, but that would
            > >> normally be done by a simple
            > >>
            > >> > if ((j % 2) == 1)
            > >>
            > >> which does indeed test to see if "j" is odd, and removes any need for a
            > >> variable - thereby removing 5 lines from the code.
            > >>
            > >> Lastly, the function has a "return false" if the browser doesn't support
            > >> getElementsByTagName, but no corresponding "return true" when it has
            > >> worked.
            > >> Presumably it should just have had a "return".
            > >>
            > >> Regards, Dave S
            > >>
            > >> ----- Original Message -----
            > >> From: "evzen.pomschar" <rokker@>
            > >> To: <JavaScript_Official@yahoogroups.com>
            > >> Sent: Friday, July 17, 2009 7:18 AM
            > >> Subject: [JavaScript] DOM Scripting by Jeremy Keith
            > >>
            > >>
            > >> >
            > >> > I am a novice in Javascript and right now going through a book by
            > >> > Jeremy
            > >> > Keith - "DOM Scripting"
            > >> >
            > >> > I would be pleased if someone could explain me one function:
            > >> >
            > >> >
            > >> >
            > >> >
            > >> > function stripeTables() {
            > >> >
            > >> > if(!document.getElementsByTagName) return false;
            > >> >
            > >> > var tables = document.getElementsByTagName('table');
            > >> >
            > >> > for(var i=0; i<tables.length; i++) {
            > >> >
            > >> > var odd = false;
            > >> >
            > >> > var rows = tables[i].getElementsByTagName('tr');
            > >> >
            > >> > for (var j=0; j<rows.length; j++) {
            > >> >
            > >> > if (odd == true) {
            > >> >
            > >> > rows[j].style.backgroundColor = "#ffc"
            > >> >
            > >> > odd = false;
            > >> >
            > >> > } else {
            > >> >
            > >> > odd = true;
            > >> >
            > >> > }
            > >> >
            > >> > }
            > >> >
            > >> > }
            > >> >
            > >> > }
            > >> >
            > >> >
            > >> >
            > >> >
            > >> > The function stripe rows in a table.
            > >> >
            > >> > I understand the rest of the syntax but cannot see any connection of
            > >> > var
            > >> > odd with the function. How does the var odd connect to <tr>? There is
            > >> > no
            > >> > visible path for var odd to <tr>. Is the var odd part of the loop?
            > >> > Forgive me but I just don't see it.
            > >> >
            > >> >
            > >> >
            > >> >
            > >> >
            > >> > I would appreciate any explanation from anyone.
            > >> >
            > >> >
            > >> >
            > >> >
            > >> > Thanks
            > >> >
            > >> >
            > >> >
            > >> >
            > >> > Ev.
            > >> >
            > >> >
            > >> >
            > >> > [Non-text portions of this message have been removed]
            > >> >
            > >> >
            > >> >
            > >> > ------------------------------------
            > >> >
            > >> > Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
            > >> >
            > >> >
            > >> >
            > >>
            > >>
            > >> --------------------------------------------------------------------------------
            > >>
            > >>
            > >>
            > >> No virus found in this incoming message.
            > >> Checked by AVG - www.avg.com
            > >> Version: 8.5.375 / Virus Database: 270.13.19/2244 - Release Date:
            > >> 07/17/09
            > >> 18:00:00
            > >>
            > >
            > >
            > >
            > >
            > > ------------------------------------
            > >
            > > Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
            > >
            > >
            > >
            >
            >
            > --------------------------------------------------------------------------------
            >
            >
            >
            > Internal Virus Database is out of date.
            > Checked by AVG - www.avg.com
            > Version: 8.5.375 / Virus Database: 270.13.11/2232 - Release Date: 07/11/09
            > 17:56:00
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.