SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript not called by pressing return key

    My javascript function setHw is not called by pressing the return key in all of the following pages.
    Please help.
    Code:
    <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>
    Code:
    <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>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Get rid of the inline javascript event (eurgh!) and use a different method instead.

    When you want it done on the enter key bring pressed, I presume that you want that so that you can run the script before the form is submitted.

    Be direct and use the onsubmit event for the form instead.

    Code javascript:
    // what to do when the form is submitted
    function tagformSubmit() {
        setHw();
    }
     
    // assign tagformSubmit to each appropriate form
    var forms = document.getElementsByTagName('form'),
        form,
        i;
    for (i = 0; i < forms.length; i += 1) {
        form = forms[i];
        if (form.name = tagform) {
            form.onsubmit = tagformSubmit;
        }
    }

    The above script should be run from just before the </body> tag.

    for example:

    Code html4strict:
    ...
        <script type="text/javascript" src="/js/script.js">
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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