...

View Full Version : Calling onClick event from HTML form button



fernandop
05-06-2009, 04:13 AM
Hello, friends!

I'm having some issues with my HTML/Javascript page. This is my 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

adios
05-06-2009, 05:16 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum