Page 80 - DCAP202_Fundamentals of Web Programming
P. 80

Unit 6: Introduction to JavaScript




          The code for obtaining a reference to a textarea element is very similar:             Notes
          oTextarea  =  oForm.elements[“textarea_element_name”];
          To get the value entered by the user in the textarea field:
          textarea_val  =  oTextarea.value;
          As an example, if we have a textarea element like this:
          <textarea  name="address"  id="txta_address"  rows="3"  cols="35"></textarea>
          We can access the value entered by the user in this way:
          address  =  oForm.elements["address"].value;

          6.7.3 Value for Hidden Element

          The code for obtaining a reference to a hidden input element:
          oHidden  =  oForm.elements["hidden_element_name"];
          To get the value of this element:
          hidden_val  =  oHidden.value;
          As an example, if we have a hidden input element in the form defined like this:
          <input  type="hidden"  name="number_of_skillsets"  value="1">
          We can get the hidden input element's value like this:
          number_of_skillsets  =  oForm.elements["number_of_skillsets"].value;

          Self Assessment

          Fill in the blanks:
          13.  To get the value of the ........................... element, we can use the value property of the text
               input object
          14.  The  code  for  obtaining  a  reference  to  a  ...........................  element  is  oTextarea  =
               oForm.elements[“textarea_element_name”].
          6.8 Type Casting


          It’s also possible to convert values using a process called type casting. Type casting allows you
          to access a specific  value as if it were of  a different type. Three  type casts  are available  in
          JavaScript:
              Boolean(value) – casts the given value as a Boolean

              Number(value) – casts the given value as a number (either integer or floating-point)
              String(value) – casts the given value a string
          Casting a value using one of these three functions creates a new value that is a direct conversion
          of the original. This can lead to some unexpected results. The Boolean() type cast returns true
          when the  value is a string with at least one character, a  number other  than 0, or an object
          (discussed in the next section); it returns false when the value is an empty string, the number 0,
          undefined , or null . The following code snippet can be used to test type casting as a Boolean:
          var  b1  =  Boolean(“”);  //false  –  empty  string
          var  b2  =  Boolean(“hi”);  //true  –  non-empty  string
          var  b3  =  Boolean(100);  //true  –  non-zero  number
          var  b4  =  Boolean(null);  //false  -  null




                                           LOVELY PROFESSIONAL UNIVERSITY                                   73
   75   76   77   78   79   80   81   82   83   84   85