tags:

views:

320

answers:

2

hi

<input type="button" onclick="ajaxFunction()" name="calculate" value='Calculate' />

i have the above on a page, if i click the button, my ajax function works fine. however, if i press "enter", the form submits but nothing happens - no doubt as the function is called "onclick"

is there any way to change this so the user may click the button, or press enter on the keyboard, and have the form submit to ajax correctly?

thanks


updated:

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }
    var gold = document.getElementById('gold_amount').value;
    var carat = document.getElementById('carat').value;

    var queryString = "?gold=" + gold + "&carat=" + carat;
    ajaxRequest.open("GET", "script.php" + queryString, true);
    ajaxRequest.send(null); 

}

//-->
+1  A: 

you have the onsubmit event on your form tag. it's raised when the form si submited (so also when you press enter).

http://www.w3schools.com/jsref/event_form_onsubmit.asp

remi bourgarel
and make the button's type as `submit`.
Anurag
please see updated code in original posti think i need to update my ajax now as the above changes, whilst logical, made the app stop workingthanks
Ross
A: 

Javascript has a method onKeydown and with that you can capture a Enter key click.

<body>
<form action="" method="POST" id="myForm">
<input type="text" name="myText" onKeyDown="changeVal()">

<script type="text/javascript" language="JavaScript">
s1 = new String(myForm.myText.value)

function changeVal() {
     s1 = "You pressed a key"
    myForm.myText.value = s1.toUpperCase() 
} 

</script>
</form>
</body>
Younes