472,146 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Code Not Working in Firefox: keyup event

I have a web page with the following items on it:

[HTML]<label for="commissionschedule">
Commission Schedule<br />
<span id="charCounter">1000 characters or less</span>
</label>
<textarea id="commissionschedule" rows="10" cols="40"></textarea>[/HTML]

I'm using a standard javascript addEvent function and am attaching a keyup event to the textarea as follows:

Expand|Select|Wrap|Line Numbers
  1. function addEvent(elm, evType, fn, useCapture)
  2. // cross browser event handling
  3. {
  4.   if (elm.addEventListener)
  5.   {
  6.    elm.addEventListener(evType, fn, useCapture);
  7.    return true;
  8.   } else if (elm.attachEvent)
  9.   {
  10.    var r = elm.attachEvent('on' + evType, fn);
  11.    return r;
  12.   } else
  13.   {
  14.    elm['on' + evType] = fn;
  15.   }
  16. }
  17.  
  18. function addListeners(e)
  19. {
  20.   var commissionSchedule = document.getElementById("commissionschedule");
  21.   addEvent(commissionSchedule, 'keyup', setCounter, false);
  22. }
  23.  
  24. function setCounter() {
  25.     var charCounter = document.getElementById("charCounter");
  26.     var commissionSchedule = document.getElementById("commissionschedule");
  27.     var currentLength = commissionSchedule.value.length;
  28.     var numCharsLeft = 1000 - currentLength;
  29.     var htmlFrag = document.createTextNode("" + numCharsLeft + " characters left");
  30.     while (charCounter.hasChildNodes())
  31.         charCounter.removeChild(charCounter.lastChild);
  32.     charCounter.appendChild(htmlFrag);
  33. }
  34.  
  35. addEvent(window, 'load', addListeners, false);
Now, I've done this a dozen times before (with 'click', though, not 'keyup'), and never had a problem, but in this case, the setCounter function is never called in Firefox. In IE this works perfectly, updating #charCounter with the number of characters left on each keyup event. However, in FF, the setCounter event handler is never even called. I have checked, and setCounter IS getting attached to the onkeyup even handler for the textarea at page load, but never actually fires once the user begins typing.

Any idea? Thanks a ton!
Aug 17 '07 #1
1 4237
mrhoo
428 256MB
Nothing wrong with your script, its the html- firefox is stricter than IE.
A label can only contain its control element and text. Try this:

[HTML]<p >
<label for="commissionschedule">Commission Schedule</label>
<span id="charCounter">1000 characters or less</span>
</p>
<textarea id="commissionschedule" rows="10" cols="40"></textarea>[/HTML]
Aug 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Lachlan Hunt | last post: by
7 posts views Thread by Coder | last post: by
1 post views Thread by Frank O'Hara | last post: by
1 post views Thread by Chris Jobson | last post: by
2 posts views Thread by Tony Johansson | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.