1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HTML Form, stop enter from reloading page

Discussion in 'JavaScript' started by crazyFoo, May 19, 2008.

  1. #1
    I'm working on an HTML form that uses AJAX to search a database and return results. Everything works just fine, except when someone hits 'Enter' instead of clicking my search button (which is NOT a true submit button, it is calling JavaScript onclick) the page reloads. This, of course completely kills the AJAX functionality.

    Does anyone have any idea how to stop the page from reloading when 'Enter' is pressed? Better yet, does anyone know how to make the 'Enter' key call my JavaScript function?

    Thanks in advance.
     
    crazyFoo, May 19, 2008 IP
  2. o0DarkEvil0o

    o0DarkEvil0o Peon

    Messages:
    11
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    let try use:

    <form onsubmit="return submit_funtion();">
    HTML:
    or if ur button id is "submitbt",
    <form onsubmit="return getElementById('submitbt').click()">
    HTML:
     
    o0DarkEvil0o, May 19, 2008 IP
  3. crazyFoo

    crazyFoo Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your help. Unfortunately, it didn't work. However, I found a workaround for the solution that involved me reorganizing how the page worked.
     
    crazyFoo, May 20, 2008 IP
  4. vurentjie

    vurentjie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can also use this

    function checkkey(e,f)
    {
    if((e == 13) || (f==13))
    {return false;}
    else
    {return true;}

    }

    onkeypress='return checkkey(event.which, event.keyCode);'

    i learnt this somewhere, cant remember where, basically checks for enter key pressed, quite handy to use it only for texboxes and then not on textareas as you would still want to have linebreaks in textareas.
     
    vurentjie, May 24, 2008 IP