Hi everyone!
I want to force the script to run a certain function even if user press enter on the input field and not the SUBMIT button.
I’ve tried something and at first it worked like a charm but then, for some reason (perhaps something I did :)
The form:
[CODE]
<form name=”searchForm” method=”get” action=”search.php”>
<p><input type=”text” name=”searchText” id=”searchText” class=”longinput” value=”” onkeypress=”searchKeyPress(event);” /></p>
<p><input type=”button” id=”btnSearch” value=”Go” class=”bfloat” style=”width:35px;” onclick=”validSearchForm();” /></p>
</form>
[COLOR=”red”]The function I use to check if the ENTER key was pressed:
[CODE]
function searchKeyPress(e){
if (window.event) {
e = window.event;
}
if (e.keyCode == 13) {
document.getElementById(‘btnSearch’).click();
[COLOR=”DarkGreen”]//which supposed to click the button as if the user did. this actually works but for some reason, it also submits the form regardless validation test passed or not.[/COLOR]
}
}
[COLOR=”Red”]The function I run onclick and I want to run when ENTER key is pressed:
[CODE]
function validSearchForm(){
var submitForm = “YES”;
var searchText = document.getElementById(‘searchText’).value;
if (searchText === “”) {
document.getElementById(‘validSearch’).innerHTML = “<strong>Search:</strong> <font color=’red’><i>enter search text</i></font>”;
var submitForm = “NO”;
}
else if (submitForm == “YES”){
document.searchForm.submit();
var submitForm = “”;
}
}
Appreciate the help!