/    Sign up×
Community /Pin to ProfileBookmark

Strange things r going on!

I can’t trigger a java-script function to alert the event.charCode form within a function by a keystroke in a textfield while using FireFox.
I got this simple function:

function onlyNumbers(){
alert(event.charCode);
}
and in my HTML part:

<input type=”text” onkeypress=”onlyNumbers()”>.Result: Nothing!

Using the same function with “keyCode” instead of “charCode” works fine in IE.

even using this function –

function onlyNumbers(){
alert(“Alert works from within this function in FireFox!”);
}

  • works nicely.
  • And to make it complete:
    <input type=”text” onkeypress=”alert(event.CharCode)”> works even in FireFox.

    But i can’t trigger this form within a function in FireFox. What’s going on?
    Help is appreciated.

    to post a comment
    JavaScript

    6 Comments(s)

    Copy linkTweet thisAlerts:
    @KravvitzSep 09.2007 — That's because you wrote the code to use IE's non-standard event model.

    I recommend you read these:

    http://www.quirksmode.org/js/contents.html#events

    http://www.brainjar.com/dhtml/events/

    http://developer.apple.com/internet/webcontent/eventmodels.html

    It's best to avoid using inline event handlers whenever possible. I suggest you read up on [url=http://www.dynamicsitesolutions.com/javascript/best-practices/#unobtrusive_javascript]Unobtrusive JavaScript and other JavaScript Best Practices[/url].

    Try this:
    function onlyNumbers(e){ <br/>
    e = e||window.event;
    var key = e.which||e.charCode||e.keyCode;
    key = String.fromCharCode(key);
    alert(key);
    }
    window.onload = function(){
    document.getElementById('myField').onkeypress=onlyNumbers;
    }
    &lt;input type="text" id="myField"&gt;
    Copy linkTweet thisAlerts:
    @TorrriateauthorSep 10.2007 — I don't get it done.

    the syntax-

    document.getElementById('myField').onkeypress=myFunction;

    -doesn't work for me. "onkeypress" ain't case sensitive, is it? But it still doesn't matter because i tried out (on a fresh page) "onkeyPress" and "onKeyPress" as well. Not workin.


    I dunno why.

    Besides, I use lots of inline-statements (on my actual page) within the body tag like -

    <body onLoad="myFunction(),...">

    -which works well and which I don't wanna abondon (would be too complicated). The prob is that the body statements are executed before the javascript - function:

    window.onload=function(){

    ..........

    }

    -so that the function is ignored.

    So i tried to implement your code by applying it form the body onLoad statement instead of from the window.onload -function. -

    function onlyNumbers(){

    alert('test');

    }

    function assign(){

    document.getElementById('myField').onkeyPress=onlyNumbers;

    }

    and in the body tag:

    <body onLoad="assign()"....

    Still not workin, still got the prob with the onkeyPress -syntax.

    But using "onKeyPress" this time I yield the result that onlyNumbers is executed independent of my keystroke, each time the page is loaded. Keystroke still doesn't apply.

    Any idea? Thanx in advance.
    Copy linkTweet thisAlerts:
    @TorrriateauthorSep 10.2007 — and found out that it's got nothin to do with the case sensitivity of onkeypress

    but with the brackets i added.

    function assign(){

    document.getElementById('myField').onkeypress= onlyNumbers();

    }

    -or

    function assign(){

    document.getElementById('myField').onkeyPress= onlyNumbers();

    }

    -or

    function assign(){

    document.getElementById('myField').onKeyPress= onlyNumbers();

    }

    makes up the result, that onlyNumbers() is exectued by loading the page independent of the keystroke. It's a riddle to me!
    Copy linkTweet thisAlerts:
    @TorrriateauthorSep 10.2007 — So anybody any different idea how to trigger onkeypress from a function using FireFox?
    Copy linkTweet thisAlerts:
    @TorrriateauthorSep 10.2007 — Thanks Kravvitz,

    I figured it out! the statement "event" is only defined within the input tag and you have to submit it to your function, which can handle it.

    f.eg.:

    <script type="javascript/text">

    function onlyNumbers(evt){

    if(evt.keyCode!=0||evt.keyCode!=null){//IE

    alert(evt.keyCode);

    }

    else alert(evt.charCode); //FireFox

    }

    </script>

    .....

    <input type="text" onkeypress="onlyNumbers(event)"/>

    .....

    Thats what I'm talkin about.... workin workin workin....

    Thanks anyway! Greets.
    Copy linkTweet thisAlerts:
    @KravvitzSep 11.2007 — Ah. Yes, the onload attribute of <body> conflicts with window.onload.

    [url=http://www.sitepoint.com/forums/showpost.php?p=3369699&postcount=18]This explains one of the other issues you had.[/url]

    *sigh* Inline events are sometimes simpler to write but that does not make them the best choice.
    ×

    Success!

    Help @Torrriate spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...