SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot trickyjw's Avatar
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting an input field to submit with the enter key

    Hello I've been trying to figure out with a search box I've added how it can submit by pressing the 'enter' key rather than just clicking a 'submit' button beside a form. I've tried onkeypress but as soon as you press one button the field submits. I've also seen you can do with keycode==13 but I'm not sure how to add it into my code. I've pasted my code in below. Any help would be much appreciated.

    Code:
    <input type="text" name="Search" id="Search" class="searchBox" />
        <a href="#" onclick="getContentURL(Search.value);"><img src="/img/magnifying-glass.gif" class="searchImg" /></a>
        <script type="text/javascript"> 
    function getContentURL(sSearch){
    window.location ="/search/pages/Results.aspx?k="+sSearch+"&s=All%20Sites";
    }
    </script>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The input field needs to be inside a form - both to be valid and for the enter key to be able to trigger whatever is attached to the action attribute of the form tag.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot trickyjw's Avatar
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great thank you for your help. Worked well.


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
  •