Results 1 to 9 of 9

Thread: Simple Javascript

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Simple Javascript

    I've got a form with a textbox. When I press Enter in the textbox, I don't want the form to submit; instead I want to call a JS function. How would I do that?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    for IE anyway:
    Code:
    fucntion Text1_KeyPress()
    {
        if (event.keyCode == 13)
        {
            event.returnValue = false;
            //call JS function here.
        }
    }
    
    <form>
        <input type="text" onKeyPress="Text1_KeyPress()"/>
    </form>
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Does it work in NS or have you just not tested it?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by filburt1
    Does it work in NS or have you just not tested it?
    Untested. I don't think I've ever tested for cross-browser compatability, I just hope for the best.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Well it works great in IE but surprise surprise, NS managed to screw it up. But there's nothing in the JS Console ("javascript:" in the location bar), it just does nothing making me assume "onKeyPress" isn't supported:

    Code:
    <script language="javascript">
    <!--
    function transferKeyEvent()
    {
        if (event.keyCode == 13)
        {
            event.returnValue = true;
            submitSearch();
        }
    }
    // -->
    </script>
    <input width="100%" type="text" onKeyPress="transferKeyEvent()" name="q">

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Well, from what I understand, NS has a totally different event handling scheme than IE, so, it wouldn't be that its not supported, just called in a different way. I haven't coded for NS in a long while, so it could have changed since then.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by filburt1
    Well it works great in IE but surprise surprise, NS managed to screw it up. But there's nothing in the JS Console ("javascript:" in the location bar), it just does nothing making me assume "onKeyPress" isn't supported:

    Code:
    <script language="javascript">
    <!--
    function transferKeyEvent()
    {
        if (event.keyCode == 13)
        {
            event.returnValue = true;
            submitSearch();
        }
    }
    // -->
    </script>
    <input width="100%" type="text" onKeyPress="transferKeyEvent()" name="q">
    On second look, you are setting event.returnValue to True, try setting it to False.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Originally posted by crptcblade
    On second look, you are setting event.returnValue to True, try setting it to False.

    Good point but no change.

  9. #9
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    This thread is going in the wrong direction.

    Here is the answer to the question

    Code:
    <script>
    function checkMyForm(the_form){
        //do some errror checking
        //if everything is ok
    	if(the_form.mytext.value == "hello world"){
             the_form.submit();
       //else if we need more input
       }else{
       		 alert("please enter the correct phrase !");
       }
       return false;
    }
    </script>
    <form name="myform" method="post" action="some.cgi" onSubmit="return checkMyForm(this)">
    <input type="text" name="mytext">
    </form>
    This is cross browser compatiable and works a treat.

    If you need me to explain how this method works and what it is doing just ask !!

Posting Permissions

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



Click Here to Expand Forum to Full Width