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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Calling onClick event from HTML form button

    Hello, friends!

    I'm having some issues with my HTML/Javascript page. This is my code:

    Code:
    <script language="JavaScript">
    function testaResultado (form) {
        var resposta = form.txtResposta.value;
        if (resposta.toUpperCase() == "GOOGLE") {
    		window.location = "pagina.html"
    	}
    	form.txtResposta.value= ""
    }
    
    function checaEnter (e, form) {
    	if ((e.which) == 13) {
    	testaResultado(form);
    	}
    }
    
    <form name="frmJogo" method="post">
    <input type="text" name="txtResposta" onKeyPress="return checaEnter(event, this.form)"> <p>
    <input type="button" name="btnOK" value="OK" onClick="testaResultado(this.form)">
    </form>
    When I type "google" on txtResposta (text object) and click on btnOK button, the pagina.html page is showed, ok. But if I press the [ENTER] key instead of clicking on btnOK button, the page is reloaded and pagina.html is not showed.

    Can I do it? Where is the mistake?

    Best regards,

    Fernando
    Last edited by fernandop; 05-06-2009 at 04:22 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Code:
    function checaEnter (e, form) {
            var keycode = e.which || e.keyCode;
    	if (keycode == 13) {
    	testaResultado(form);
            return false;
    	}
            else return true;
    }
    Otherwise this: onKeyPress="return checaEnter
    ... doesn't do anything.

    Somewhat different event object properties cross-browser.

    http://cross-browser.com/talk/event_interface_soup.php


  •  

    Posting Permissions

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