My javascript function setHw is not called by pressing the return key in all of the following pages.
Please help.

<div id="rowPerPageDiv">
<form name="tagform" action="Hw.htm" method="post">
  <input type="hidden" name="currPage" value="0"/>
  Rows Per Page<input type="text" name="rowsPerPage" id="rowsPerPage" value="20" onsubmit="return setHw()"/>
  <input type="button" value="Apply" onClick="setHw()"><br>
</form>
</div>
<div id="rowPerPageDiv">
<form name="tagform">
  <input type="hidden" name="currPage" value="0"/>
  Rows Per Page<input type="text" name="rowsPerPage" id="rowsPerPage" value="20" onsubmit="return setHw()"/>
  <input type="button" value="Apply" onClick="setHw()"><br>
</form>
</div>

Dani AI

Generated

The root cause is that onsubmit belongs on the form, not the text input, and a plain type="button" does not create the browser submit target that pressing Enter uses in many browsers. was on the right track about moving the submit handler to the form; ’s suggestion to catch key events also works but is better done with a keydown listener and modern event handling. The simplest, most robust fixes are: make the Apply control a submit button (so Enter triggers the form), or attach a key handler to the text field that explicitly calls setHw() and prevents the default submit.

A compact, cross-browser approach (place this script after the form or run on DOMContentLoaded):

<form id="tagform" action="Hw.htm" method="post">
  <input type="hidden" name="currPage" value="0" />
  Rows Per Page <input type="text" id="rowsPerPage" name="rowsPerPage" value="20" />
  <input type="submit" value="Apply" />
</form>

<script>
(function () {
  var form = document.getElementById('tagform');
  var field = document.getElementById('rowsPerPage');

  form.addEventListener('submit', function (e) {
    e.preventDefault(); // stop actual navigation when handling in JS
    setHw();
  });

  field.addEventListener('keydown', function (e) {
    var key = e.key || e.keyCode || e.which;
    if (key === 'Enter' || key === 13) {
      e.preventDefault();
      setHw();
    }
  });
})();
</script>

Troubleshooting checklist: ensure setHw is defined before the handlers run (or assign it to window.setHw), check the browser console for script errors, avoid duplicate IDs/names, and prefer keydown/e.key === 'Enter' for reliable Enter detection. If you want the form to submit to Hw.htm after setHw, remove e.preventDefault() (or have setHw return true).

Recommended Answers

All 2 Replies

The "onsubmit" should be in the <form> tag - try that. If you want to catch the enter key being hit in a text field, you should use a function that checks the key being pressed on keyup/keydown, such as:

function enterKeyHit (e)
    {
    if (e && e.keyCode == 13)
        {
        return true;
        }
    else
        {
        return false;
        }
    }

<input type="text" name="rowsPerPage" id="rowsPerPage" value="20" onkeyup="if (enterKeyHit(event)) {setHw();}"/>
Member Avatar for Member #334542
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="javascript">
function showDiv(e) {
	var unicode=e.keyCode? e.keyCode : e.charCode
	if (unicode==13){
		//your code
	}
}
</script>
</head>

<body>
<body onkeypress="showDiv(event)">
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.