Page 147 - CITS - Computer Software Application -TT
P. 147

COMPUTER SOFTWARE APPLICATION - CITS



              <script type=”text/javascript”>

              function printvalue(){
              var name=document.form1.name.value;
              alert(“Welcome: “+name);
              }
              </script>

              <form name=”form1”>
              Enter Name:<input type=”text” name=”name”/>
              <input type=”button” onclick=”printvalue()” value=”print name”/>
              </form>

           the  document.getElementById()  method  returns  the  element  of  specified  id.In  the  previous  page,  we  have
           used document.form1.name.value to get the value of the input value. Instead of this, we can use document.
           getElementById() method to get value of the input text. But we need to define id for the input field. Let’s see the
           simple example of document.getElementById() method that prints cube of the given number.
              <script type=”text/javascript”>
              function getcube(){

              var number=document.getElementById(“number”).value;
              alert(number*number*number);
              }
              </script>

              <form>
              Enter No:<input type=”text” id=”number” name=”number”/><br/>
              <input type=”button” value=”cube” onclick=”getcube()”/>
              </form>
           Example of document.getElementsByName() method
           In this example, we going to count total number of genders. Here, we are using getElementsByName() method
           to get all the genders.
              <script type=”text/javascript”>
              function totalelements()
              {
              var allgenders=document.getElementsByName(“gender”);
              alert(“Total Genders:”+allgenders.length);
              }
              </script>
              <form>
              Male:<input type=”radio” name=”gender” value=”male”>
              Female:<input type=”radio” name=”gender” value=”female”>
               <input type=”button” onclick=”totalelements()” value=”Total Genders”>

              </form>
           Example of document.getElementsByTagName() method
           In this example, we going to count total number of paragraphs used in the document. To do this, we have called
           the document.getElementsByTagName(“p”) method that returns the total paragraphs.



                                                           134

                             CITS : IT&ITES - Computer  Software Application - Lesson 37 - 46
   142   143   144   145   146   147   148   149   150   151   152