SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript question on reset and onfocus

    1. How to reset textbox + textarea?
    2. How to make the "function getKeyValue_h(chr)" work without the submit button? (on pasting in textarea it should automatically give the calculated result in the textbox.)

    JavaScript:
    Code:
    <!-- 
    //&&& 1st of 2 instances of getKeyValue(). changed name of Fx, '_h' for hebrew? 
    function getKeyValue_h(chr) { 
      chr=chr.charAt(); 
      if(chr=="A") return 1; 
      if(chr=="B") return 2; 
      if(chr=="G") return 3; 
      if(chr=="D") return 4; 
      if(chr=="H") return 5; 
      if(chr=="V") return 6; 
      if(chr=="X") return 8; 
      if(chr=="+") return 9; 
      if(chr=="Y") return 10; 
      if(chr=="k" || chr=="K") return 20; 
      if(chr=="L") return 30; 
      if(chr=="m" || chr=="M") return 40; 
      if(chr=="n" || chr=="N") return 50; 
      if(chr=="$") return 60; 
      if(chr=="O") return 70; 
      if(chr=="p" || chr=="P") return 80; 
      if(chr=="c" || chr=="C") return 90; 
      if(chr=="q") return 100; 
      if(chr=="R") return 200; 
      if(chr=="S") return 300; 
      if(chr=="T") return 400; 
      return 0; 
    } 
    
    
    function computeValue(str) { 
      var ans=0; 
      for(var i=0; i<str.length; i++) { 
        ans+=getKeyValue(str.charAt(i)); 
      } 
      return ans; 
    }
    //-->
    Within <body> (does it have to be in a form too?):
    Code:
    <br><font face="BSTHebrew">
    <textarea name="hInput" id="hInput" rows="9" cols="64"
     class="onLoad" onmouseover="this.className='onMouseOver'"
     onmouseout="this.className='onMouseOut'">
    </textarea></font><br>
     
    <input type="button" value="calculate value" onclick="javascript:computeValue(document.all.hInput.value, 'h');"> 
    
    <input type="reset" value="Clear" name="B2">
    
    <input type="text" name="hOutput" id="hOutput" readonly="true">
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Form elements should always be in a form. "javascript:" is unneeded in event handlers.

    Code:
    function computeValue(str) { 
      var ans=0; 
      for(var i=0; i<str.length; i++) { 
        ans+=getKeyValue(str.charAt(i)); 
      } 
      document.getElementById('hOutput').value = ans;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    didn't work

    THe code you gave me didn't work.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title></title>
    <script type="text/javascript"><!--
    //&&& 1st of 2 instances of getKeyValue(). changed name of Fx, '_h' for hebrew? 
    function getKeyValue_h(chr) { 
      chr=chr.charAt(); 
      if(chr=="A") return 1; 
      if(chr=="B") return 2; 
      if(chr=="G") return 3; 
      if(chr=="D") return 4; 
      if(chr=="H") return 5; 
      if(chr=="V") return 6; 
      if(chr=="X") return 8; 
      if(chr=="+") return 9; 
      if(chr=="Y") return 10; 
      if(chr=="k" || chr=="K") return 20; 
      if(chr=="L") return 30; 
      if(chr=="m" || chr=="M") return 40; 
      if(chr=="n" || chr=="N") return 50; 
      if(chr=="$") return 60; 
      if(chr=="O") return 70; 
      if(chr=="p" || chr=="P") return 80; 
      if(chr=="c" || chr=="C") return 90; 
      if(chr=="q") return 100; 
      if(chr=="R") return 200; 
      if(chr=="S") return 300; 
      if(chr=="T") return 400; 
      return 0; 
    } 
    
    
    function computeValue(str) { 
      var ans=0; 
      for(var i=0; i<str.length; i++) { 
        ans+=getKeyValue_h(str.charAt(i)); // the '_h' was missing in your code
      } 
      //return ans; 
    document.getElementById('hOutput').value = ans;
    }
    // -->
    </script>
    </head>
    <body>
    
    <br><font face="BSTHebrew">
    <textarea name="hInput" id="hInput" rows="9" cols="64"
     class="onLoad" onmouseover="this.className='onMouseOver'"
     onmouseout="this.className='onMouseOut'">
    </textarea></font><br>
     
    <input type="button" value="calculate value" onclick="computeValue(document.getElementById('hInput').value, 'h');"> 
    
    <input type="reset" value="Clear" name="B2">
    
    <input type="text" name="hOutput" id="hOutput" readonly="true">
    
    </body>
    </html>
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •