Forcing action on hitting 'return' in textfield

Posted on 2007-09-28
Last Modified: 2011-10-03
Is there a way to do the following:

When a user enters info into a textfield and hits RETURN/ENTER a script calls.

There will be no form tag, because I do not want it to submit, just call the javascript action.

<input name="info" type="text" id="info" size="40"/ onEnter="JavaScript:alert("TEST");">

Basically I want it to act like it normally would when being submitted in a form tag, just without the form tag, and calling the alert instead.
Question by:mopar003
    LVL 63

    Expert Comment

    You can filter the event codes which are different objects for IE and Netscape or you add a form tag and kill the submit action.
    Like this:
    <form onSubmit="JavaScript:alert("TEST");return false;">
    <input name="info" type="text" id="info" size="40" >

    LVL 63

    Accepted Solution

    Sorry, it should be other quote chars:

    <form onSubmit='JavaScript:alert("TEST");return false;' >
    <input name="info" type="text" id="info" size="40" >

    LVL 1

    Author Comment

    Perfect.  It was the 'return false' that I was missing.  Thanks for the quick response.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now