Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Loughborough (UK)
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS Problem In FireFox

    Hi all,

    Could anybody give me a clue as to what is wrong with this (simple) piece of javascript that prevents it working in FireFox and other non-IE browsers???


    <script type="text/javascript">
    function CheckInputLength()
    {
    var txtMaxLength = document.getElementById("txtMaxLength");

    if (txtMaxLength != null)
    {
    var txtUserInput = document.getElementById("txtUserInput");
    var lblCharRemain = document.getElementById("lblCharRemain");
    var intMaxLength = eval(txtMaxLength.value);
    var intCurLength = eval(txtUserInput.value.length);
    var intRemain = intMaxLength - intCurLength;

    lblCharRemain.innerText = intRemain.toString();
    }
    }
    </script>

    <body onkeyup="CheckInputLength();">

    <input name="txtMaxLength" type="text" value="600" id="txtMaxLength" />

    <textarea name="txtUserInput" id="txtUserInput">test</textarea>

    Characters Remaining: <span id="lblCharRemain">?</span>

    </body>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Change lblCharRemain.innerText to lblCharRemain.innerHTML
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,695
    Thanks
    0
    Thanked 232 Times in 227 Posts
    Hi there christrinder,

    I would suggest that you change innerText to firstChild.nodeValue, remove the evals and place the onkeyup within the script.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    window.onload=function() {
    document.body.onkeyup=function(){
       CheckInputLength();
      }
     }
    function CheckInputLength(){
       txtMaxLength = document.getElementById("txtMaxLength");
    
    if(txtMaxLength!= null){
       txtUserInput=document.getElementById("txtUserInput");
       lblCharRemain=document.getElementById("lblCharRemain");
       intMaxLength=txtMaxLength.value;
       intCurLength=txtUserInput.value.length;
       intRemain=intMaxLength-intCurLength;
    
       lblCharRemain.firstChild.nodeValue= intRemain.toString();
      }
     }
    </script>
    
    </head>
    <body>
    
    <div>
    
    <input id="txtMaxLength" name="txtMaxLength" type="text" value="600" />
    
    <textarea id="txtUserInput" name="txtUserInput" rows="5" cols="20">test</textarea>
    
    Characters Remaining: <span id="lblCharRemain">?</span>
    
    </div>
    
    </body>
    </html>
    coothead


  •  

    Posting Permissions

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