Javascript onClick Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • risk32
    New Member
    • Mar 2008
    • 98

    Javascript onClick Error

    Hi all. I have a problem with an onClick function working when I click a button. The infamous IE error reporting links it back to the button itself, but I don't think that's the case (Line 48, 49 - Object doesn't support this property or method). The code itself isn't complete since I just started and can finish the rest later. Anyways, here's what I have so far...

    Code:
    <html>
    <script type="text/javascript">
    // if zulu - > time is checked
       var hour1 = document.time.getElementByName("hour").value;
       var minute1 = document.time.getElementByName("minute").value;
       parseInt(hour1);
       parseInt(minute1);
    
      function local()
       {
       
    if (hour1 > 05 & < 23)
       {
          hour1 = hour1 - 5;
       }
    else if (hour1 = 00)
       {
          hour1 = 19;
       }
    else if (hour1 = 01) //line 20
       {
          hour1 = 20;
       }
    else if (hour1 = 02)
       {
          hour1 = 21;
       }
    else if (hour1 = 03)
       {
          hour1 = 22;
       }
    else if (hour1 = 04)
       {
          hour1 = 23;
       }
     else
       {
          hour1 = 00;
       }
    document.time.hour= hour1 //line 40
    }
    </script>
    <form name=time>
    <center>
    <input type="text" maxlength=2 size=2 id="hour" name="hour" ></input> : 
    <input type="text" maxlength=2 size=2 id="minute" name="minute"></input>
    <br>
    <input type="button" id="local" value="Local -> Zulu" onClick="zulu()"></input>
    <input type="button" id="zulu" value = "Zulu -> Local" onClick="local()"></input><br>
    </form>
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you have errors on line 4, 5 & 12:
    Code:
    getElement[B][U]s[/U][/B]ByName()
    Code:
    if (hour1 > 05 & < 23) ???
    
    // maybe you mean
    if (hour1 > 05 && hour1 < 23)

    Comment

    • risk32
      New Member
      • Mar 2008
      • 98

      #3
      Dormilich,
      There's still an error somewhere. I changed the &&. Also, why would I use the getElements when there is only one element to get a value from?

      Comment

      • freddieMaize
        New Member
        • Aug 2008
        • 85

        #4
        Originally posted by risk32
        Also, why would I use the getElements when there is only one element to get a value from?
        :) Because that is the syntax

        Freddie

        Comment

        • risk32
          New Member
          • Mar 2008
          • 98

          #5
          freddieMaize,
          There's also a syntax for a single element, getElementBy(Id , Name, Type...)

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by risk32
            There's also a syntax for a single element, getElementBy(Id , Name, Type...)
            there isn’t*. (see ref.) everything else is getElementsBy*( )

            * - because the id attribute is the only one defined as unique (see html DTD).

            Comment

            • risk32
              New Member
              • Mar 2008
              • 98

              #7
              Ah ok.. the getElement is only for ID while getElements is for Names. I got the code to produce no errors, but it's not updating the text box. I'm researching how to use objects to do this.

              Code:
                  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
              <html>
              <head>
              <meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see www.w3.org">
              <script type="text/javascript">
              // if zulu - > time is checked
                 
              
                function local()
                 {
              
                 var hour1; 
                 hour1 = document.getElementsByName("hour").value;
                 var minute1; 
                 minute1 = document.getElementsByName("minute").value;
                 parseInt(hour1);
                 parseInt(minute1);
                 
              if (hour1 > 05 && < 23)
                 {
                    hour1 = hour1 - 5;
                 }
              else if (hour1 = 00)
                 {
                    hour1 = 19;
                 }
              else if (hour1 = 01)
                 {
                    hour1 = 20;
                 }
              else if (hour1 = 02)
                 {
                    hour1 = 21;
                 }
              else if (hour1 = 03)
                 {
                    hour1 = 22;
                 }
              else if (hour1 = 04)
                 {
                    hour1 = 23;
                 }
               else
                 {
                    hour1 = 00;
                 }
              parseString(hour1);
              parseString(minute1);
              document.hour.write(hour1);
                 }
                 
                 
                 function zulu()
                 {
                 var hour2; 
                 hour2 = document.getElementsByName("hour").value;
                 var minute2;
                 minute2 = document.getElementsByName("minute").value;
                 parseInt(hour2);
                 parseInt(minute2);
              
                 if (hour2 < 19)
                   {
                      hour2 = hour2 + 5;
                   }
                 
                 else if (hour2 = 20)
                   {
                      hour2 = 01;
                   }
                 
                 else if (hour2 = 21)
                   {
                      hour2 = 02;
                 else if (hour2 = 22)
                   {
                      hour2 = 03;
                   }
                 else if (hour2 = 23)
                   {
                      hour2 = 04;
                   }
              parseString(hour2);
              parseString(minute2);
              document.hour.write(hour2);
                 }
              </script>
              <title></title>
              </head>
              <body>
              <center>
              
              <input type="text" name="hour" maxlength="2" size="2">:
              <input type="text" name="minute" maxlength="2" size="2"><br>
              <input type="button" id="local" value="Local -&gt; Zulu" onclick="function zulu()">
              <input type="button" id="zulu" value="Zulu -&gt; Local" onclick="function local()">
              <br></center>
              </body>
              </html>

              Comment

              • risk32
                New Member
                • Mar 2008
                • 98

                #8
                OK... I've gotten rid of the BS I don't need in my code. There's no point of messing with the minute portion of it, so I took it out. Hopefully I did the parseInt() portion of it right. After doing the mathematical functions to the recently parsed strings, I need to convert the integers back to strings to input them in the text box, right?? I'm kinda lost. The document.write( hour) doesn't seem to be working. I would imagine that it's doing nothing with it, because I am not specifiying where exactly to write the string. I'm so close to being done, but yet so far away. According to IE, there's an object that is missing, but I can't figure out what it is.

                Code:
                <html>
                <script type="text/javascript">
                // if zulu - > time is checked
                 
                function localToZulu()
                   {
                  var hour = document.getElementByValue("hour");
                  parseInt(hour);
                if (hour > 05 && < 23)
                   {
                      hour = hour - 5;
                   }
                else if (hour == 00)
                   {
                      hour = 19;
                   }
                else if (hour == 01)
                   {
                      hour = 20;
                   }
                else if (hour == 02)
                   {
                      hour = 21;
                   }
                else if (hour == 03)
                   {
                      hour = 22;
                   }
                else if (hour == 04)
                   {
                      hour = 23;
                   }
                 else
                   {
                      hour = 00;
                   }
                
                }
                
                stringHour = parseString(hour);
                document.hour.write(stringHour);
                
                // if local time is checked
                   function zuluToLocal()
                   {
                 var hour = document.getElementByName("hour");
                 parseInt(hour);
                   if (hour < 19)
                     {
                        hour = hour + 5;
                     }
                   
                   else if (hour == 20)
                     {
                        hour = 01;
                     }
                   
                   else if (hour == 21)
                     {
                        hour = 02;
                     }
                   else if (hour == 22)
                     {
                        hour = 03;
                     }
                   else if (hour == 23)
                     {
                        hour = 04;
                     }
                }
                
                stringHour = parseString(hour);
                document.hour.write(stringHour);
                
                </script>
                <center>
                &nbsp;Hour Minute<br><input type="text" size=2 maxlength=2 name="hour">
                <input type="text" size=2 maxlength=2 name="minute">
                <br>
                <input type="button" value="Local" onClick="zuluToLocal()">
                <input type="button" value="Zulu" onClick="localToZulu()">
                </center>
                </html>

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Code:
                  function localToZulu()
                  {
                  // this method does not exist
                    var hour = document.[U]getElementByValue[/U]("hour"); 
                  
                  // hour is not affected by parseInt(), you need to assign the return value
                    parseInt(hour);
                  
                  // invalid syntax, operand missing
                  if (hour > 05 &&[U] < 23[/U])
                   
                  …
                  }
                  
                  // not necessary here, JavaScript does type conversation automatically
                  stringHour = parseString(hour);
                  
                  // write() is a method of document only
                  document.hour.write(stringHour);
                  
                  function zuluToLocal()
                  {
                  // this method does not exist
                  var hour = document.[U]getElementByName[/U]("hour");
                  …
                  }
                  just out of interest, do you have any JavaScript reference work where you can look up?

                  Comment

                  • risk32
                    New Member
                    • Mar 2008
                    • 98

                    #10
                    My reference material is limited due to the fact I'm on a government computer.
                    I'll look at home to see if I can find anything. I'm used to using Java so JS seems comfusing to me in some aspects. The amount you can do in JS is limiting.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Originally posted by risk32
                      I'm used to using Java so JS seems comfusing to me in some aspects.
                      Java and JavaScript hardly have anything in common. Don’t try to conclude from one to the other, this will only get you in trouble.

                      for instance:
                      while Java’s inheritance is class based, JavaScript’s inheritance is prototype based (see wikipedia for explanation).
                      you can add/alter/delete properties and methods in JavaScript anytime you want, even at runtime.

                      Originally posted by risk32
                      The amount you can do in JS is limiting.
                      you have hardly an idea what you actually can do with JavaScript. You’d be surprised…

                      JavaScripts possibilities depend on the JS parser used (this may be a browser, but I’ve also seen a game engine using it)

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        Originally posted by risk32
                        ... I'm used to using Java so JS seems comfusing to me in some aspects. The amount you can do in JS is limiting. ....
                        Just by not knowing a language doesn't mean that it is limiting per se ... it is just limiting the 'unknowing' people ... i.e. if i don't know how to speak zulu it is limiting me! unless i have learned it ... but it isn't a limiting language itself because all native 'zulu'-speakers can have very unlimited conversations :) JavaScript has much greater potential as most people think ... even though it does have its limits of course ... like any other programming language has.

                        kind regards

                        Comment

                        • risk32
                          New Member
                          • Mar 2008
                          • 98

                          #13
                          OK...OK.... I'll admit it... I'm a fool when it comes to JS. I've only been able to pick up bits and pieces from other's work (no, i don't use what they code... that's pretty much stealing when it's not open source) and from reference material I just so happen to stumble upon when doing a google search. With the code I'm trying to produce, basically just simple math then displaying the number back into the text back, do I have the right basic idea?

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by risk32
                            With the code I'm trying to produce, basically just simple math then displaying the number back into the text back, do I have the right basic idea?
                            mmmmh……… no.

                            replacing text in form elements is (mostly) simple:
                            Code:
                            // [I]input[/I] is the corresponding <input> element
                            [I]input[/I].value = "new_value";
                            if you want one single element, use an id:
                            Code:
                            // the html element
                            <input id="box1" type="text" name="…" … >
                            
                            // now in JS
                            var input = document.getElementById("box1");
                            
                            // read what’s typed in
                            var usertext = input.value;
                            // if you want an integer
                            var userint = parseInt(input.value);
                            if you do comparisons, the syntax is always ([] - optional):
                            [ ( ] operand operator operand [ ) ]
                            ex:
                            x < 1 && x > -1 or ((x < 1) && (x > -1))
                            note that the result of one of those comparison expressions is evaluated as boolean.

                            Comment

                            • risk32
                              New Member
                              • Mar 2008
                              • 98

                              #15
                              So basically, I create a variable to capture the input from the text box (I used "hour" for the Id)...
                              Code:
                              var hour1 = document.getElementById("hour");
                              Seems simple enough... When replacing the text like you said, would I be in the righrt to do this?
                              Code:
                              var text = hour1.value;
                              If it's right, how would I use the text variable to replace what's in the text box?

                              Comment

                              Working...