Replace a character in an input box - onkeypress - FF problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonySAN
    New Member
    • May 2007
    • 3

    #1

    Replace a character in an input box - onkeypress - FF problem

    Hi,
    I have the following code which works fine in IE but does not in Firefox:
    Code:
    <html> <head>
         <title>space-plus</title>
    <script type="text/javascript">
    <!--
    function alpha(e) {
         if(e.keyCode==32) {return (e.keyCode=43);}
    }
    // -->
    </script>
    </head>  <body> <form>
    <input type="text" onkeypress="return alpha(event);">
    </form> </body> </html>
    I have tried a number of variations - without success. The idea is that when a user enters a space in the input box a plus sign is shown.
    Any answers? Can Firefox do this?
    Last edited by acoder; Jan 19 '13, 09:53 PM.
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    i believe in firefox its charCode
    so you could do something like
    Code:
    <html> <head>
    <title>space-plus</title>
    <script type="text/javascript">
    <!--
    function alpha(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if (unicode==32) { return (e.charCode ? e.charCode=43 : e.keyCode=43); }
    }
    // -->
    </script>
    </head> <body> <form>
    <input type="text" onkeypress="return alpha(event);">
    </form> </body> </html>

    Comment

    • tonySAN
      New Member
      • May 2007
      • 3

      #3
      Thanks Clint,
      works in IE but still not in Firefox!
      I wonder if the problem lies in intercepting the keycode and sending on the new keycode (or charcode). I have also tried 'onkeyup' and 'onkeydown' but these stop the function working in IE.
      Your code suggestion was:
      Code:
      <html> <head>
      <title>space-plus</title>
      <script type="text/javascript">
      <!--
      function alpha(e) {
      var unicode = e.charCode ? e.charCode : e.keyCode;
      if (unicode==32) { return (e.charCode ? e.charCode=43 : e.keyCode=43); }
      }
      // -->
      </script>
      </head> <body> <form>
      <input type="text" onkeypress="return alpha(event);">
      </form> </body> </html>

      Comment

      • Logician
        New Member
        • Feb 2007
        • 210

        #4
        Originally posted by tonySAN
        The idea is that when a user enters a space in the input box a plus sign is shown.
        Any answers? Can Firefox do this?
        <input type='text' onkeyup="this.v alue=this.value .replace(/\s/g,'+')">

        Comment

        • tonySAN
          New Member
          • May 2007
          • 3

          #5
          Thank you Logician. Does the job! (In both browsers.)
          TS.

          Comment

          • chetannagar
            New Member
            • Feb 2008
            • 1

            #6
            Originally posted by iam_clint
            i believe in firefox its charCode
            so you could do something like
            Code:
            <html> <head>
            <title>space-plus</title>
            <script type="text/javascript">
            <!--
            function alpha(e) {
            var unicode = e.charCode ? e.charCode : e.keyCode;
            if (unicode==32) { return (e.charCode ? e.charCode=43 : e.keyCode=43); }
            }
            // -->
            </script>
            </head> <body> <form>
            <input type="text" onkeypress="return alpha(event);">
            </form> </body> </html>
            This code help me a lot for creating unicode in textbox.

            Chetan

            Comment

            • kul1010
              New Member
              • Jan 2013
              • 1

              #7
              Pankaj

              Originally posted by Logician
              <input type='text' onkeyup="this.v alue=this.value .replace(/\s/g,'+')">


              very easy and good. Thank you Logician.
              Last edited by Rabbit; Jan 19 '13, 07:40 PM. Reason: Fixed quote.

              Comment

              Working...